This repository incorporates a curated checklist of JavaScript algorithms, organized by class. These vary from easy string manipulation to superior looking out and sorting strategies — excellent for interviews and foundational studying.
Word
Recognition is predicated on widespread interview matters, academic supplies, and developer group utilization.
operate reverseString(str) {
return str.break up("").reverse().be a part of("");
}
console.log(reverseString("hey")); // Output: "olleh"
Rationalization: Reverses the characters in a string by splitting, reversing, and becoming a member of them again collectively.
operate isPalindrome(str) {
return str === str.break up("").reverse().be a part of("");
}
console.log(isPalindrome("racecar")); // Output: true
Rationalization: Determines if a string reads the identical backward as ahead utilizing string reversal.
operate charFrequency(str) {
const freq = {};
for (let char of str) 0) + 1;
return freq;
}
console.log(charFrequency("hey")); // Output: { h: 1, e: 1, l: 2, o: 1 }
Rationalization: Counts how usually every character seems in a string.
operate isAnagram(str1, str2) {
const normalize = (str) => str.break up("").type().be a part of("");
return normalize(str1) === normalize(str2);
}
console.log(isAnagram("pay attention", "silent")); // Output: true
Rationalization: Determines if two strings are anagrams by sorting and evaluating them.
operate isPrime(num) {
if (num <= 1) return false;
for (let i = 2; i <= Math.sqrt(num); i++) {
if (num % i === 0) return false;
}
return true;
}
console.log(isPrime(7)); // Output: true
Rationalization: Checks if a quantity is prime by testing divisibility as much as its sq. root.
operate fibonacci(n) {
if (n <= 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
console.log(fibonacci(6)); // Output: 8
Rationalization: Generates the nth Fibonacci quantity recursively by summing the 2 previous numbers.
O(2^n)
and is inefficient for big inputs.
operate factorial(n) {
if (n === 0) return 1;
return n * factorial(n - 1);
}
console.log(factorial(5)); // Output: 120
Rationalization: Calculates the factorial of a quantity recursively by multiplying it with decremented values.
operate gcd(a, b) {
if (b === 0) return a;
return gcd(b, a % b);
}
console.log(gcd(48, 18)); // Output: 6
Rationalization: Makes use of the Euclidean algorithm to compute the best widespread divisor.
operate twoSum(nums, goal) {
const map = new Map();
for (let i = 0; i < nums.size; i++) {
const complement = goal - nums[i];
if (map.has(complement)) return [map.get(complement), i];
map.set(nums[i], i);
}
return [];
}
console.log(twoSum([2, 7, 11, 15], 9)); // Output: [0, 1]
Rationalization: Finds two indices such that their values sum to the goal utilizing a hash map.
operate binarySearch(arr, goal) {
let left = 0,
proper = arr.size - 1;
whereas (left <= proper) {
const mid = Math.ground((left + proper) / 2);
if (arr[mid] === goal) return mid;
if (arr[mid] < goal) left = mid + 1;
else proper = mid - 1;
}
return -1;
}
console.log(binarySearch([1, 2, 3, 4, 5], 4)); // Output: 3
Rationalization: Searches for a goal in a sorted array utilizing a divide-and-conquer strategy.
operate bubbleSort(arr) {
for (let i = 0; i < arr.size; i++) {
for (let j = 0; j < arr.size - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
[arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
}
}
}
return arr;
}
console.log(bubbleSort([5, 3, 8, 4, 2])); // Output: [2, 3, 4, 5, 8]
Rationalization: Kinds an array by repeatedly swapping adjoining components if they’re within the flawed order.
operate quickSort(arr) {
if (arr.size <= 1) return arr;
const pivot = arr[arr.length - 1];
const left = [],
proper = [];
for (let i = 0; i < arr.size - 1; i++) {
if (arr[i] < pivot) left.push(arr[i]);
else proper.push(arr[i]);
}
return [...quickSort(left), pivot, ...quickSort(right)];
}
console.log(quickSort([3, 6, 8, 10, 1, 2, 1])); // Output: [1, 1, 2, 3, 6, 8, 10]
Rationalization: A divide-and-conquer sorting algorithm with an average-case time complexity of O(n log n)
.
operate mergeSortedArrays(arr1, arr2) {
let merged = [], i = 0, j = 0;
whereas (i < arr1.size && j < arr2.size) {
if (arr1[i] < arr2[j]) {
merged.push(arr1[i++]);
} else {
merged.push(arr2[j++]);
}
}
return merged.concat(arr1.slice(i)).concat(arr2.slice(j));
}
console.log(mergeSortedArrays([1, 3, 5], [2, 4, 6])); // Output: [1, 2, 3, 4, 5, 6]
Rationalization: Merges two sorted arrays into one sorted array by evaluating components sequentially.
operate findMax(arr) {
return Math.max(...arr);
}
console.log(findMax([1, 2, 3, 4, 5])); // Output: 5
Rationalization: Finds the most important quantity in an array utilizing the Math.max
operate and unfold operator.
operate debounce(fn, delay) {
let timer;
return operate (...args) {
clearTimeout(timer);
timer = setTimeout(() => fn.apply(this, args), delay);
};
}
const log = debounce(() => console.log("Debounced!"), 300);
log();
log();
log(); // Logs as soon as after 300ms of inactivity
Rationalization: Limits the speed at which a operate can hearth, generally utilized in occasion dealing with (e.g., enter, scroll).