Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d518fd0

Browse files
Merge pull request #4 from AllThingsSmitty/feature_update
Add grouping
2 parents d04ddfb + 9e66249 commit d518fd0

File tree

1 file changed

+168
-90
lines changed

1 file changed

+168
-90
lines changed

‎README.md‎

Lines changed: 168 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,48 @@
1-
# JavaScript Algorithms (Sorted by Popularity)
1+
# JavaScript Algorithms (Grouped by Type)
22

3-
This list showcases some of the most popular JavaScript algorithms, from simple string manipulations to classic recursive solutions and efficient searching techniques. Each snippet demonstrates a fundamental concept often encountered in coding interviews and real-world development.
3+
This repository contains a curated list of JavaScript algorithms, organized by category. These range from simple string manipulation to advanced searching and sorting techniques — perfect for interviews and foundational learning.
44

5-
> **Note:** Popularity is based on common interview topics, educational resources, and usage in developer communities.
5+
> [!Note]
6+
> Popularity is based on common interview topics, educational materials, and developer community usage.
67
7-
## 1. Reverse a String
8+
## Algorithms
9+
10+
### String Manipulation
11+
12+
- [Reverse a String](#reverse-a-string)
13+
- [Palindrome Check](#palindrome-check)
14+
- [Character Frequency Counter](#character-frequency-counter)
15+
- [Anagram Check](#anagram-check)
16+
17+
### Math & Number Theory
18+
19+
- [Prime Number Check](#prime-number-check)
20+
- [Fibonacci Sequence (Recursive)](#fibonacci-sequence-recursive)
21+
- [Factorial of a Number](#factorial-of-a-number)
22+
- [Find the GCD (Greatest Common Divisor)](#find-the-gcd-greatest-common-divisor)
23+
24+
### Searching
25+
26+
- [Two Sum (Using Hash Map)](#two-sum-using-hash-map)
27+
- [Binary Search](#binary-search)
28+
29+
### Sorting
30+
31+
- [Bubble Sort](#bubble-sort)
32+
- [Quick Sort](#quick-sort)
33+
- [Merge Two Sorted Arrays](#merge-two-sorted-arrays)
34+
35+
### Array Utilities
36+
37+
- [Find Maximum in Array](#find-maximum-in-array)
38+
39+
### Utility Functions
40+
41+
- [Debounce Function](#debounce-function)
42+
43+
## String Manipulation
44+
45+
### Reverse a String
846

947
```js
1048
function reverseString(str) {
@@ -16,7 +54,9 @@ console.log(reverseString("hello")); // Output: "olleh"
1654

1755
**Explanation**: Reverses the characters in a string by splitting, reversing, and joining them back together.
1856

19-
## 2. Palindrome Check
57+
<sup>[Back to top](#algorithms)</sup>
58+
59+
### Palindrome Check
2060

2161
```js
2262
function isPalindrome(str) {
@@ -28,55 +68,44 @@ console.log(isPalindrome("racecar")); // Output: true
2868

2969
**Explanation**: Determines if a string reads the same backward as forward using string reversal.
3070

31-
## 3. Binary Search
71+
<sup>[Back to top](#algorithms)</sup>
72+
73+
### Character Frequency Counter
3274

3375
```js
34-
function binarySearch(arr, target) {
35-
let left = 0,
36-
right = arr.length - 1;
37-
while (left <= right) {
38-
const mid = Math.floor((left + right) / 2);
39-
if (arr[mid] === target) return mid;
40-
if (arr[mid] < target) left = mid + 1;
41-
else right = mid - 1;
76+
function charFrequency(str) {
77+
const freq = {};
78+
for (let char of str) {
79+
freq[char] = (freq[char] || 0) + 1;
4280
}
43-
return -1;
81+
return freq;
4482
}
4583

46-
console.log(binarySearch([1, 2, 3, 4, 5], 4)); // Output: 3
84+
console.log(charFrequency("hello")); // Output: { h: 1, e: 1, l: 2, o: 1 }
4785
```
4886

49-
**Explanation**: Searches for a target in a sorted array using a divide-and-conquer approach.
87+
**Explanation**: Counts how often each character appears in a string.
5088

51-
## 4. Fibonacci Sequence (Recursive)
89+
<sup>[Back to top](#algorithms)</sup>
90+
91+
### Anagram Check
5292

5393
```js
54-
function fibonacci(n) {
55-
if (n <=1) return n;
56-
return fibonacci(n -1) +fibonacci(n -2);
94+
function isAnagram(str1, str2) {
95+
constnormalize= (str) =>str.split("").sort().join("");
96+
return normalize(str1) ===normalize(str2);
5797
}
5898

59-
console.log(fibonacci(6)); // Output: 8
99+
console.log(isAnagram("listen", "silent")); // Output: true
60100
```
61101

62-
**Explanation**: Generates the nth Fibonacci number recursively by summing the two preceding numbers.
63-
64-
⚠️ **Note**: This approach has exponential time complexity `O(2^n)` and is inefficient for large inputs.
65-
66-
## 5. Factorial of a Number
67-
68-
```js
69-
function factorial(n) {
70-
if (n === 0) return 1;
71-
return n * factorial(n - 1);
72-
}
102+
**Explanation**: Determines if two strings are anagrams by sorting and comparing them.
73103

74-
console.log(factorial(5)); // Output: 120
75-
```
104+
<sup>[Back to top](#algorithms)</sup>
76105

77-
**Explanation**: Calculates the factorial of a number recursively by multiplying it with decremented values.
106+
## Math & Number Theory
78107

79-
##6. Prime Number Check
108+
### Prime Number Check
80109

81110
```js
82111
function isPrime(num) {
@@ -92,58 +121,41 @@ console.log(isPrime(7)); // Output: true
92121

93122
**Explanation**: Checks if a number is prime by testing divisibility up to its square root.
94123

95-
## 7. Find Maximum in Array
124+
<sup>[Back to top](#algorithms)</sup>
125+
126+
### Fibonacci Sequence (Recursive)
96127

97128
```js
98-
function findMax(arr) {
99-
return Math.max(...arr);
129+
function fibonacci(n) {
130+
if (n <= 1) return n;
131+
return fibonacci(n - 1) + fibonacci(n - 2);
100132
}
101133

102-
console.log(findMax([1, 2, 3, 4, 5])); // Output: 5
134+
console.log(fibonacci(6)); // Output: 8
103135
```
104136

105-
**Explanation**: Finds the largest number in an array using the `Math.max` function and spread operator.
106-
107-
## 8. Merge Two Sorted Arrays
108-
109-
```js
110-
function mergeSortedArrays(arr1, arr2) {
111-
let merged = [], i = 0, j = 0;
112-
while (i < arr1.length && j < arr2.length) {
113-
if (arr1[i] < arr2[j]) {
114-
merged.push(arr1[i++]);
115-
} else {
116-
merged.push(arr2[j++]);
117-
}
118-
}
119-
return merged.concat(arr1.slice(i)).concat(arr2.slice(j));
120-
}
137+
**Explanation**: Generates the nth Fibonacci number recursively by summing the two preceding numbers.
121138

122-
console.log(mergeSortedArrays([1, 3, 5], [2, 4, 6])); // Output: [1, 2, 3, 4, 5, 6]
123-
```
139+
<sup>[Back to top](#algorithms)</sup>
124140

125-
**Explanation**: Merges two sorted arrays into one sorted array by comparing elements sequentially.
141+
⚠️ **Note**: This approach has exponential time complexity `O(2^n)` and is inefficient for large inputs.
126142

127-
##9. Bubble Sort
143+
### Factorial of a Number
128144

129145
```js
130-
function bubbleSort(arr) {
131-
for (let i = 0; i < arr.length; i++) {
132-
for (let j = 0; j < arr.length - i - 1; j++) {
133-
if (arr[j] > arr[j + 1]) {
134-
[arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
135-
}
136-
}
137-
}
138-
return arr;
146+
function factorial(n) {
147+
if (n === 0) return 1;
148+
return n * factorial(n - 1);
139149
}
140150

141-
console.log(bubbleSort([5, 3, 8, 4, 2])); // Output: [2, 3, 4, 5, 8]
151+
console.log(factorial(5)); // Output: 120
142152
```
143153

144-
**Explanation**: Sorts an array by repeatedly swapping adjacent elements if they are in the wrong order.
154+
**Explanation**: Calculates the factorial of a number recursively by multiplying it with decremented values.
145155

146-
## 10. Find the GCD (Greatest Common Divisor)
156+
<sup>[Back to top](#algorithms)</sup>
157+
158+
### Find the GCD (Greatest Common Divisor)
147159

148160
```js
149161
function gcd(a, b) {
@@ -156,7 +168,11 @@ console.log(gcd(48, 18)); // Output: 6
156168

157169
**Explanation**: Uses the Euclidean algorithm to compute the greatest common divisor.
158170

159-
## 11. Two Sum (Using Hash Map)
171+
<sup>[Back to top](#algorithms)</sup>
172+
173+
## Searching
174+
175+
### Two Sum (Using Hash Map)
160176

161177
```js
162178
function twoSum(nums, target) {
@@ -174,36 +190,54 @@ console.log(twoSum([2, 7, 11, 15], 9)); // Output: [0, 1]
174190

175191
**Explanation**: Finds two indices such that their values sum to the target using a hash map.
176192

177-
## 12. Anagram Check
193+
<sup>[Back to top](#algorithms)</sup>
194+
195+
### Binary Search
178196

179197
```js
180-
function isAnagram(str1, str2) {
181-
const normalize = (str) => str.split("").sort().join("");
182-
return normalize(str1) === normalize(str2);
198+
function binarySearch(arr, target) {
199+
let left = 0,
200+
right = arr.length - 1;
201+
while (left <= right) {
202+
const mid = Math.floor((left + right) / 2);
203+
if (arr[mid] === target) return mid;
204+
if (arr[mid] < target) left = mid + 1;
205+
else right = mid - 1;
206+
}
207+
return -1;
183208
}
184209

185-
console.log(isAnagram("listen", "silent")); // Output: true
210+
console.log(binarySearch([1, 2, 3, 4, 5], 4)); // Output: 3
186211
```
187212

188-
**Explanation**: Determines if two strings are anagrams by sorting and comparing them.
213+
**Explanation**: Searches for a target in a sorted array using a divide-and-conquer approach.
214+
215+
<sup>[Back to top](#algorithms)</sup>
189216

190-
## 13. Character Frequency Counter
217+
## Sorting
218+
219+
### Bubble Sort
191220

192221
```js
193-
function charFrequency(str) {
194-
const freq = {};
195-
for (let char of str) {
196-
freq[char] = (freq[char] || 0) + 1;
222+
function bubbleSort(arr) {
223+
for (let i = 0; i < arr.length; i++) {
224+
for (let j = 0; j < arr.length - i - 1; j++) {
225+
if (arr[j] > arr[j + 1]) {
226+
[arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
227+
}
228+
}
197229
}
198-
return freq;
230+
return arr;
199231
}
200232

201-
console.log(charFrequency("hello")); // Output: { h: 1, e: 1, l: 2, o: 1 }
233+
console.log(bubbleSort([5, 3, 8, 4, 2])); // Output: [2, 3, 4, 5, 8]
202234
```
203235

204-
**Explanation**: Counts how often each character appears in a string.
236+
**Explanation**: Sorts an array by repeatedly swapping adjacent elements if they are in the wrong order.
237+
238+
<sup>[Back to top](#algorithms)</sup>
205239

206-
##14. Quick Sort
240+
### Quick Sort
207241

208242
```js
209243
function quickSort(arr) {
@@ -223,7 +257,49 @@ console.log(quickSort([3, 6, 8, 10, 1, 2, 1])); // Output: [1, 1, 2, 3, 6, 8, 10
223257

224258
**Explanation**: A divide-and-conquer sorting algorithm with an average-case time complexity of `O(n log n)`.
225259

226-
## 15. Debounce Function
260+
<sup>[Back to top](#algorithms)</sup>
261+
262+
### Merge Two Sorted Arrays
263+
264+
```js
265+
function mergeSortedArrays(arr1, arr2) {
266+
let merged = [], i = 0, j = 0;
267+
while (i < arr1.length && j < arr2.length) {
268+
if (arr1[i] < arr2[j]) {
269+
merged.push(arr1[i++]);
270+
} else {
271+
merged.push(arr2[j++]);
272+
}
273+
}
274+
return merged.concat(arr1.slice(i)).concat(arr2.slice(j));
275+
}
276+
277+
console.log(mergeSortedArrays([1, 3, 5], [2, 4, 6])); // Output: [1, 2, 3, 4, 5, 6]
278+
```
279+
280+
**Explanation**: Merges two sorted arrays into one sorted array by comparing elements sequentially.
281+
282+
<sup>[Back to top](#algorithms)</sup>
283+
284+
## Array Utilities
285+
286+
### Find Maximum in Array
287+
288+
```js
289+
function findMax(arr) {
290+
return Math.max(...arr);
291+
}
292+
293+
console.log(findMax([1, 2, 3, 4, 5])); // Output: 5
294+
```
295+
296+
**Explanation**: Finds the largest number in an array using the `Math.max` function and spread operator.
297+
298+
<sup>[Back to top](#algorithms)</sup>
299+
300+
## Utility Functions
301+
302+
### Debounce Function
227303

228304
```js
229305
function debounce(fn, delay) {
@@ -241,3 +317,5 @@ log(); // Logs once after 300ms of inactivity
241317
```
242318

243319
**Explanation**: Limits the rate at which a function can fire, commonly used in event handling (e.g., input, scroll).
320+
321+
<sup>[Back to top](#algorithms)</sup>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /