|
1 | | -# javascript-algorithms |
| 1 | +# JavaScript Algorithms (Sorted by Popularity) |
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. |
| 4 | + |
| 5 | +## 1. Reverse a String |
| 6 | + |
| 7 | +```js |
| 8 | +function reverseString(str) { |
| 9 | + return str.split("").reverse().join(""); |
| 10 | +} |
| 11 | + |
| 12 | +console.log(reverseString("hello")); // Output: "olleh" |
| 13 | +``` |
| 14 | + |
| 15 | +**Explanation**: Reverses the characters in a string by splitting, reversing, and joining them back together. |
| 16 | + |
| 17 | +## 2. Palindrome Check |
| 18 | + |
| 19 | +```js |
| 20 | +function isPalindrome(str) { |
| 21 | + return str === str.split("").reverse().join(""); |
| 22 | +} |
| 23 | + |
| 24 | +console.log(isPalindrome("racecar")); // Output: true |
| 25 | +``` |
| 26 | + |
| 27 | +**Explanation**: Determines if a string reads the same backward as forward using string reversal. |
| 28 | + |
| 29 | +## 3. Binary Search |
| 30 | + |
| 31 | +```js |
| 32 | +function binarySearch(arr, target) { |
| 33 | + let left = 0, |
| 34 | + right = arr.length - 1; |
| 35 | + while (left <= right) { |
| 36 | + const mid = Math.floor((left + right) / 2); |
| 37 | + if (arr[mid] === target) return mid; |
| 38 | + if (arr[mid] < target) left = mid + 1; |
| 39 | + else right = mid - 1; |
| 40 | + } |
| 41 | + return -1; |
| 42 | +} |
| 43 | + |
| 44 | +console.log(binarySearch([1, 2, 3, 4, 5], 4)); // Output: 3 |
| 45 | +``` |
| 46 | + |
| 47 | +**Explanation**: Efficiently searches for a target in a sorted array using a divide-and-conquer approach. |
| 48 | + |
| 49 | +## 4. Fibonacci Sequence |
| 50 | + |
| 51 | +```js |
| 52 | +function fibonacci(n) { |
| 53 | + if (n <= 1) return n; |
| 54 | + return fibonacci(n - 1) + fibonacci(n - 2); |
| 55 | +} |
| 56 | + |
| 57 | +console.log(fibonacci(6)); // Output: 8 |
| 58 | +``` |
| 59 | + |
| 60 | +**Explanation**: Generates the nth Fibonacci number recursively by summing the two preceding numbers. |
| 61 | + |
| 62 | +## 5. Factorial of a Number |
| 63 | + |
| 64 | +```js |
| 65 | +function factorial(n) { |
| 66 | + if (n === 0) return 1; |
| 67 | + return n * factorial(n - 1); |
| 68 | +} |
| 69 | + |
| 70 | +console.log(factorial(5)); // Output: 120 |
| 71 | +``` |
| 72 | + |
| 73 | +**Explanation**: Calculates the factorial of a number recursively by multiplying it with decremented values. |
| 74 | + |
| 75 | +## 6. Prime Number Check |
| 76 | + |
| 77 | +```js |
| 78 | +function isPrime(num) { |
| 79 | + if (num <= 1) return false; |
| 80 | + for (let i = 2; i <= Math.sqrt(num); i++) { |
| 81 | + if (num % i === 0) return false; |
| 82 | + } |
| 83 | + return true; |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +**Explanation**: Checks if a number is prime by testing divisibility up to its square root. |
| 88 | + |
| 89 | +## 7. Find Maximum in Array |
| 90 | + |
| 91 | +```js |
| 92 | +function findMax(arr) { |
| 93 | + return Math.max(...arr); |
| 94 | +} |
| 95 | + |
| 96 | +console.log(findMax([1, 2, 3, 4, 5])); // Output: 5 |
| 97 | +``` |
| 98 | + |
| 99 | +**Explanation**: Finds the largest number in an array using the `Math.max` function and the spread operator. |
| 100 | + |
| 101 | +## 8. Merge Two Sorted Arrays |
| 102 | + |
| 103 | +```js |
| 104 | +function mergeSortedArrays(arr1, arr2) { |
| 105 | + let merged = [], |
| 106 | + i = 0, |
| 107 | + j = 0; |
| 108 | + while (i < arr1.length && j < arr2.length) { |
| 109 | + if (arr1[i] < arr2[j]) { |
| 110 | + merged.push(arr1[i]); |
| 111 | + i++; |
| 112 | + } else { |
| 113 | + merged.push(arr2[j]); |
| 114 | + j++; |
| 115 | + } |
| 116 | + } |
| 117 | + return merged.concat(arr1.slice(i)).concat(arr2.slice(j)); |
| 118 | +} |
| 119 | + |
| 120 | +console.log(mergeSortedArrays([1, 3, 5], [2, 4, 6])); // Output: [1, 2, 3, 4, 5, 6] |
| 121 | +``` |
| 122 | + |
| 123 | +**Explanation**: Combines two sorted arrays into one sorted array by comparing elements sequentially. |
| 124 | + |
| 125 | +## 9. Bubble Sort |
| 126 | + |
| 127 | +```js |
| 128 | +function bubbleSort(arr) { |
| 129 | + for (let i = 0; i < arr.length; i++) { |
| 130 | + for (let j = 0; j < arr.length - i - 1; j++) { |
| 131 | + if (arr[j] > arr[j + 1]) { |
| 132 | + [arr[j], arr[j + 1]] = [arr[j + 1], arr[j]]; |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + return arr; |
| 137 | +} |
| 138 | + |
| 139 | +console.log(bubbleSort([5, 3, 8, 4, 2])); // Output: [2, 3, 4, 5, 8] |
| 140 | +``` |
| 141 | + |
| 142 | +**Explanation**: Sorts an array by repeatedly swapping adjacent elements if they are in the wrong order. |
| 143 | + |
| 144 | +## 10. Find the GCD (Greatest Common Divisor) |
| 145 | + |
| 146 | +```js |
| 147 | +function gcd(a, b) { |
| 148 | + if (b === 0) return a; |
| 149 | + return gcd(b, a % b); |
| 150 | +} |
| 151 | + |
| 152 | +console.log(gcd(48, 18)); // Output: 6 |
| 153 | +``` |
| 154 | + |
| 155 | +**Explanation:** Uses the Euclidean algorithm to compute the greatest common divisor of two numbers. |
0 commit comments