Skip to content

Commit d04ddfb

Browse files
Merge pull request #3 from AllThingsSmitty/feature_update
Expand set
2 parents d80a8c5 + 694e327 commit d04ddfb

File tree

1 file changed

+93
-9
lines changed

1 file changed

+93
-9
lines changed

README.md

Lines changed: 93 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function binarySearch(arr, target) {
4646
console.log(binarySearch([1, 2, 3, 4, 5], 4)); // Output: 3
4747
```
4848

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.
5050

5151
## 4. Fibonacci Sequence (Recursive)
5252

@@ -61,7 +61,7 @@ console.log(fibonacci(6)); // Output: 8
6161

6262
**Explanation**: Generates the nth Fibonacci number recursively by summing the two preceding numbers.
6363

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.
6565

6666
## 5. Factorial of a Number
6767

@@ -102,7 +102,7 @@ function findMax(arr) {
102102
console.log(findMax([1, 2, 3, 4, 5])); // Output: 5
103103
```
104104

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.
106106

107107
## 8. Merge Two Sorted Arrays
108108

@@ -111,11 +111,9 @@ function mergeSortedArrays(arr1, arr2) {
111111
let merged = [], i = 0, j = 0;
112112
while (i < arr1.length && j < arr2.length) {
113113
if (arr1[i] < arr2[j]) {
114-
merged.push(arr1[i]);
115-
i++;
114+
merged.push(arr1[i++]);
116115
} else {
117-
merged.push(arr2[j]);
118-
j++;
116+
merged.push(arr2[j++]);
119117
}
120118
}
121119
return merged.concat(arr1.slice(i)).concat(arr2.slice(j));
@@ -143,7 +141,7 @@ function bubbleSort(arr) {
143141
console.log(bubbleSort([5, 3, 8, 4, 2])); // Output: [2, 3, 4, 5, 8]
144142
```
145143

146-
**Explanation**: Sorts an array by repeatedly swapping adjacent elements if they are in the wrong order (Time complexity: O(n²)).
144+
**Explanation**: Sorts an array by repeatedly swapping adjacent elements if they are in the wrong order.
147145

148146
## 10. Find the GCD (Greatest Common Divisor)
149147

@@ -156,4 +154,90 @@ function gcd(a, b) {
156154
console.log(gcd(48, 18)); // Output: 6
157155
```
158156

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

Comments
 (0)