@@ -46,7 +46,7 @@ function binarySearch(arr, target) {
46
46
console .log (binarySearch ([1 , 2 , 3 , 4 , 5 ], 4 )); // Output: 3
47
47
```
48
48
49
- ** Explanation** : Searches for a target in a sorted array using a divide-and-conquer approach (Time complexity: O(log n)) .
49
+ ** Explanation** : Searches for a target in a sorted array using a divide-and-conquer approach.
50
50
51
51
## 4. Fibonacci Sequence (Recursive)
52
52
@@ -61,7 +61,7 @@ console.log(fibonacci(6)); // Output: 8
61
61
62
62
** Explanation** : Generates the nth Fibonacci number recursively by summing the two preceding numbers.
63
63
64
- ⚠️ ** Note** : This approach has exponential time complexity O(2^n) and is inefficient for large inputs. Use memoization or iteration for better performance .
64
+ ⚠️ ** Note** : This approach has exponential time complexity ` O(2^n) ` and is inefficient for large inputs.
65
65
66
66
## 5. Factorial of a Number
67
67
@@ -102,7 +102,7 @@ function findMax(arr) {
102
102
console .log (findMax ([1 , 2 , 3 , 4 , 5 ])); // Output: 5
103
103
```
104
104
105
- ** Explanation** : Finds the largest number in an array using the ` Math.max ` function and the spread operator.
105
+ ** Explanation** : Finds the largest number in an array using the ` Math.max ` function and spread operator.
106
106
107
107
## 8. Merge Two Sorted Arrays
108
108
@@ -111,11 +111,9 @@ function mergeSortedArrays(arr1, arr2) {
111
111
let merged = [], i = 0 , j = 0 ;
112
112
while (i < arr1 .length && j < arr2 .length ) {
113
113
if (arr1[i] < arr2[j]) {
114
- merged .push (arr1[i]);
115
- i++ ;
114
+ merged .push (arr1[i++ ]);
116
115
} else {
117
- merged .push (arr2[j]);
118
- j++ ;
116
+ merged .push (arr2[j++ ]);
119
117
}
120
118
}
121
119
return merged .concat (arr1 .slice (i)).concat (arr2 .slice (j));
@@ -143,7 +141,7 @@ function bubbleSort(arr) {
143
141
console .log (bubbleSort ([5 , 3 , 8 , 4 , 2 ])); // Output: [2, 3, 4, 5, 8]
144
142
```
145
143
146
- ** Explanation** : Sorts an array by repeatedly swapping adjacent elements if they are in the wrong order (Time complexity: O(n2)) .
144
+ ** Explanation** : Sorts an array by repeatedly swapping adjacent elements if they are in the wrong order.
147
145
148
146
## 10. Find the GCD (Greatest Common Divisor)
149
147
@@ -156,4 +154,90 @@ function gcd(a, b) {
156
154
console .log (gcd (48 , 18 )); // Output: 6
157
155
```
158
156
159
- ** Explanation** : Uses the Euclidean algorithm to compute the greatest common divisor of two numbers (Time complexity: O(log min(a, b))).
157
+ ** Explanation** : Uses the Euclidean algorithm to compute the greatest common divisor.
158
+
159
+ ## 11. Two Sum (Using Hash Map)
160
+
161
+ ``` js
162
+ function twoSum (nums , target ) {
163
+ const map = new Map ();
164
+ for (let i = 0 ; i < nums .length ; i++ ) {
165
+ const complement = target - nums[i];
166
+ if (map .has (complement)) return [map .get (complement), i];
167
+ map .set (nums[i], i);
168
+ }
169
+ return [];
170
+ }
171
+
172
+ console .log (twoSum ([2 , 7 , 11 , 15 ], 9 )); // Output: [0, 1]
173
+ ```
174
+
175
+ ** Explanation** : Finds two indices such that their values sum to the target using a hash map.
176
+
177
+ ## 12. Anagram Check
178
+
179
+ ``` js
180
+ function isAnagram (str1 , str2 ) {
181
+ const normalize = (str ) => str .split (" " ).sort ().join (" " );
182
+ return normalize (str1) === normalize (str2);
183
+ }
184
+
185
+ console .log (isAnagram (" listen" , " silent" )); // Output: true
186
+ ```
187
+
188
+ ** Explanation** : Determines if two strings are anagrams by sorting and comparing them.
189
+
190
+ ## 13. Character Frequency Counter
191
+
192
+ ``` js
193
+ function charFrequency (str ) {
194
+ const freq = {};
195
+ for (let char of str) {
196
+ freq[char] = (freq[char] || 0 ) + 1 ;
197
+ }
198
+ return freq;
199
+ }
200
+
201
+ console .log (charFrequency (" hello" )); // Output: { h: 1, e: 1, l: 2, o: 1 }
202
+ ```
203
+
204
+ ** Explanation** : Counts how often each character appears in a string.
205
+
206
+ ## 14. Quick Sort
207
+
208
+ ``` js
209
+ function quickSort (arr ) {
210
+ if (arr .length <= 1 ) return arr;
211
+ const pivot = arr[arr .length - 1 ];
212
+ const left = [],
213
+ right = [];
214
+ for (let i = 0 ; i < arr .length - 1 ; i++ ) {
215
+ if (arr[i] < pivot) left .push (arr[i]);
216
+ else right .push (arr[i]);
217
+ }
218
+ return [... quickSort (left), pivot, ... quickSort (right)];
219
+ }
220
+
221
+ console .log (quickSort ([3 , 6 , 8 , 10 , 1 , 2 , 1 ])); // Output: [1, 1, 2, 3, 6, 8, 10]
222
+ ```
223
+
224
+ ** Explanation** : A divide-and-conquer sorting algorithm with an average-case time complexity of ` O(n log n) ` .
225
+
226
+ ## 15. Debounce Function
227
+
228
+ ``` js
229
+ function debounce (fn , delay ) {
230
+ let timer;
231
+ return function (... args ) {
232
+ clearTimeout (timer);
233
+ timer = setTimeout (() => fn .apply (this , args), delay);
234
+ };
235
+ }
236
+
237
+ const log = debounce (() => console .log (" Debounced!" ), 300 );
238
+ log ();
239
+ log ();
240
+ log (); // Logs once after 300ms of inactivity
241
+ ```
242
+
243
+ ** Explanation** : Limits the rate at which a function can fire, commonly used in event handling (e.g., input, scroll).
0 commit comments