1
- # JavaScript Algorithms (Sorted by Popularity )
1
+ # JavaScript Algorithms (Grouped by Type )
2
2
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 .
4
4
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.
6
7
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
8
46
9
47
``` js
10
48
function reverseString (str ) {
@@ -16,7 +54,9 @@ console.log(reverseString("hello")); // Output: "olleh"
16
54
17
55
** Explanation** : Reverses the characters in a string by splitting, reversing, and joining them back together.
18
56
19
- ## 2. Palindrome Check
57
+ <sup >[ Back to top] ( #algorithms ) </sup >
58
+
59
+ ### Palindrome Check
20
60
21
61
``` js
22
62
function isPalindrome (str ) {
@@ -28,55 +68,44 @@ console.log(isPalindrome("racecar")); // Output: true
28
68
29
69
** Explanation** : Determines if a string reads the same backward as forward using string reversal.
30
70
31
- ## 3. Binary Search
71
+ <sup >[ Back to top] ( #algorithms ) </sup >
72
+
73
+ ### Character Frequency Counter
32
74
33
75
``` 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 ;
42
80
}
43
- return - 1 ;
81
+ return freq ;
44
82
}
45
83
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 }
47
85
```
48
86
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 .
50
88
51
- ## 4. Fibonacci Sequence (Recursive)
89
+ <sup >[ Back to top] ( #algorithms ) </sup >
90
+
91
+ ### Anagram Check
52
92
53
93
``` 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
+ const normalize = ( str ) => str . split ( " " ). sort (). join ( " " ) ;
96
+ return normalize (str1) === normalize (str2 );
57
97
}
58
98
59
- console .log (fibonacci ( 6 )); // Output: 8
99
+ console .log (isAnagram ( " listen " , " silent " )); // Output: true
60
100
```
61
101
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.
73
103
74
- console .log (factorial (5 )); // Output: 120
75
- ```
104
+ <sup >[ Back to top] ( #algorithms ) </sup >
76
105
77
- ** Explanation ** : Calculates the factorial of a number recursively by multiplying it with decremented values.
106
+ ## Math & Number Theory
78
107
79
- ##6. Prime Number Check
108
+ ### Prime Number Check
80
109
81
110
``` js
82
111
function isPrime (num ) {
@@ -92,58 +121,41 @@ console.log(isPrime(7)); // Output: true
92
121
93
122
** Explanation** : Checks if a number is prime by testing divisibility up to its square root.
94
123
95
- ## 7. Find Maximum in Array
124
+ <sup >[ Back to top] ( #algorithms ) </sup >
125
+
126
+ ### Fibonacci Sequence (Recursive)
96
127
97
128
``` 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 );
100
132
}
101
133
102
- console .log (findMax ([ 1 , 2 , 3 , 4 , 5 ] )); // Output: 5
134
+ console .log (fibonacci ( 6 )); // Output: 8
103
135
```
104
136
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.
121
138
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 >
124
140
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 .
126
142
127
- ##9. Bubble Sort
143
+ ### Factorial of a Number
128
144
129
145
``` 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 );
139
149
}
140
150
141
- console .log (bubbleSort ([ 5 , 3 , 8 , 4 , 2 ] )); // Output: [2, 3, 4, 5, 8]
151
+ console .log (factorial ( 5 )); // Output: 120
142
152
```
143
153
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 .
145
155
146
- ## 10. Find the GCD (Greatest Common Divisor)
156
+ <sup >[ Back to top] ( #algorithms ) </sup >
157
+
158
+ ### Find the GCD (Greatest Common Divisor)
147
159
148
160
``` js
149
161
function gcd (a , b ) {
@@ -156,7 +168,11 @@ console.log(gcd(48, 18)); // Output: 6
156
168
157
169
** Explanation** : Uses the Euclidean algorithm to compute the greatest common divisor.
158
170
159
- ## 11. Two Sum (Using Hash Map)
171
+ <sup >[ Back to top] ( #algorithms ) </sup >
172
+
173
+ ## Searching
174
+
175
+ ### Two Sum (Using Hash Map)
160
176
161
177
``` js
162
178
function twoSum (nums , target ) {
@@ -174,36 +190,54 @@ console.log(twoSum([2, 7, 11, 15], 9)); // Output: [0, 1]
174
190
175
191
** Explanation** : Finds two indices such that their values sum to the target using a hash map.
176
192
177
- ## 12. Anagram Check
193
+ <sup >[ Back to top] ( #algorithms ) </sup >
194
+
195
+ ### Binary Search
178
196
179
197
``` 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 ;
183
208
}
184
209
185
- console .log (isAnagram ( " listen " , " silent " )); // Output: true
210
+ console .log (binarySearch ([ 1 , 2 , 3 , 4 , 5 ], 4 )); // Output: 3
186
211
```
187
212
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 >
189
216
190
- ## 13. Character Frequency Counter
217
+ ## Sorting
218
+
219
+ ### Bubble Sort
191
220
192
221
``` 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
+ }
197
229
}
198
- return freq ;
230
+ return arr ;
199
231
}
200
232
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]
202
234
```
203
235
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 >
205
239
206
- ##14. Quick Sort
240
+ ### Quick Sort
207
241
208
242
``` js
209
243
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
223
257
224
258
** Explanation** : A divide-and-conquer sorting algorithm with an average-case time complexity of ` O(n log n) ` .
225
259
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
227
303
228
304
``` js
229
305
function debounce (fn , delay ) {
@@ -241,3 +317,5 @@ log(); // Logs once after 300ms of inactivity
241
317
```
242
318
243
319
** 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