A fast, practical reference for solving algorithm and data structure problems.
Whether you're doing coding interviews, solving LeetCode/Codewars, or building personal projects, this JavaScript cheat sheet collects the most useful methods, patterns, and tricks for real-world problem-solving.
1. Strings
✅ Reverse a String
const reverse = str => str.split('').reverse().join('');
✅ Check Palindrome
const isPalindrome = str => str === str.split('').reverse().join('');
✅ Count Characters
const countChars = str =>
[...str].reduce((acc, ch) => {
acc[ch] = (acc[ch] || 0) + 1;
return acc;
}, {});
✅ Remove Non-Alphanumeric
const clean = s => s.replace(/[^a-z0-9]/gi, '');
Useful Methods
- str.includes()
- str.indexOf()
- str.slice(start, end)
- str.toLowerCase(), toUpperCase()
- str.replace(pattern, value)
2. Arrays
✅ Sum of Array
const sum = arr => arr.reduce((a, b) => a + b, 0);
✅ Find Max/Min
Math.max(...arr);
Math.min(...arr);
✅ Remove Duplicates
const unique = arr => [...new Set(arr)];
✅ Count Frequency
const freq = arr =>
arr.reduce((map, val) => {
map[val] = (map[val] || 0) + 1;
return map;
}, {});
Useful Methods
- map(), filter(), reduce()
- some(), every()
- find(), findIndex()
- sort()
- flat(), flatMap()
3. Objects
✅ Convert Object → Array of Pairs
Object.entries(obj);
✅ Deep Clone Object
const clone = obj => structuredClone(obj);
✅ Merge Objects
const merged = {...obj1, ...obj2};
Useful Methods
- Object.keys()
- Object.values()
- JSON.stringify(), JSON.parse()
4. Numbers & Math
✅ Check Even/Odd
n % 2 === 0; // even
✅ Generate Random Integer
const rand = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;
Useful Math Methods
- Math.floor(), Math.ceil()
- Math.round()
- Math.abs()
- Math.sqrt()
- Math.pow()
5. Sorting
Numeric Sort (Ascending / Descending)
arr.sort((a, b) => a - b);
arr.sort((a, b) => b - a);
Sort Objects by Property
arr.sort((a, b) => a.age - b.age);
6. Algorithms Patterns
6.1 Two Pointers
Efficient for sorted arrays, palindromes, etc.
Example: Two Sum (Sorted Array)
const twoSum = (arr, target) => {
let l = 0, r = arr.length - 1;
while (l < r) {
const sum = arr[l] + arr[r];
if (sum === target) return [l, r];
sum < target ? l++ : r--;
}
return null;
};
6.2 Sliding Window
Used for substring search, longest sequence, etc.
Example: Longest Substring Without Repeating Characters
const longestUnique = s => {
let set = new Set(), l = 0, max = 0;
for (let r = 0; r < s.length; r++) {
while (set.has(s[r])) set.delete(s[l++]);
set.add(s[r]);
max = Math.max(max, set.size);
}
return max;
};
6.3 Hash Map / Frequency Map
Great for counting or matching.
Example: Anagram Check
const isAnagram = (a, b) => {
if (a.length !== b.length) return false;
const map = {};
for (let ch of a) map[ch] = (map[ch] || 0) + 1;
for (let ch of b) {
if (!map[ch]) return false;
map[ch]--;
}
return true;
};
6.4 Recursion
Useful for trees, graphs, permutations.
Example: Factorial
const fact = n => (n <= 1 ? 1 : n * fact(n - 1));
Example: DFS on Tree
const dfs = node => {
if (!node) return;
console.log(node.val);
dfs(node.left);
dfs(node.right);
};
7. Promises & Async/Await
Wait for Async Task
const delay = ms => new Promise(res => setTimeout(res, ms));
Fetch Data
const getData = async url => {
const res = await fetch(url);
return await res.json();
};
8. Useful One-Liners
Convert Number → Binary
(42).toString(2);
Flatten Array
arr.flat(Infinity);
Shuffle Array
const shuffle = arr => arr.sort(() => Math.random() - 0.5);
Get Unique Array of Objects (by key)
const uniqueBy = (arr, key) => [
...new Map(arr.map(item => [item[key], item])).values()
];
Conclusion
This cheat sheet is designed to make problem-solving faster and easier by providing:
- Essential JS methods
- Proven algorithm patterns
- Ready-to-use code snippets
- Interview-focused utilities