Learning Data Structures and Algorithms (DSA) in JavaScript ๐
DSA in JavaScript
A structured roadmap to master DSA using JavaScript. Includes core concepts, code examples, and resources.
Prerequisites
JavaScript Basics:
Variables, loops, conditionals.
Functions, scope, closures.
Arrays, Objects, ES6+ features (
class
,let/const
, arrow functions).Basic recursion.
Core Data Structures
1. Arrays & Strings
Arrays: Manipulation methods (
slice
,splice
,map
,filter
).Strings: Methods like
split()
,substring()
, and pattern matching.
2. Linked Lists
class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
class LinkedList {
constructor() {
this.head = null;
}
// Add methods like append(), prepend(), delete()
}
3. Stacks & Queues
Stack (LIFO):
class Stack { constructor() { this.items = []; } push(item) { this.items.push(item); } pop() { return this.items.pop(); } }
Queue (FIFO):
class Queue { constructor() { this.items = []; } enqueue(item) { this.items.push(item); } dequeue() { return this.items.shift(); } }
4. Trees & Graphs
Binary Tree Node:
class TreeNode { constructor(value) { this.value = value; this.left = null; this.right = null; } }
Graph (Adjacency List):
class Graph { constructor() { this.nodes = new Map(); // Key: node, Value: array of neighbors } }
5. Hash Tables
Use JavaScript
Map
orObject
:const hashMap = new Map(); hashMap.set('key', 'value');
6. Heaps
Min-Heap:
class MinHeap { constructor() { this.heap = []; } // Methods: insert(), extractMin(), heapify() }
Core Algorithms
1. Sorting
Quick Sort:
function quickSort(arr) { if (arr.length <= 1) return arr; const pivot = arr[0]; const left = [], right = []; for (let i = 1; i < arr.length; i++) { arr[i] < pivot ? left.push(arr[i]) : right.push(arr[i]); } return [...quickSort(left), pivot, ...quickSort(right)]; }
2. Searching
Binary Search:
function binarySearch(arr, target) { let left = 0, right = arr.length - 1; while (left <= right) { const mid = Math.floor((left + right) / 2); if (arr[mid] === target) return mid; arr[mid] < target ? left = mid + 1 : right = mid - 1; } return -1; }
3. Recursion
Fibonacci:
function fibonacci(n) { if (n <= 1) return n; return fibonacci(n - 1) + fibonacci(n - 2); }
4. Graph Algorithms
BFS:
function bfs(graph, startNode) { const queue = [startNode]; const visited = new Set(); while (queue.length > 0) { const node = queue.shift(); if (!visited.has(node)) { visited.add(node); for (const neighbor of graph.get(node)) { queue.push(neighbor); } } } return visited; }
Practice & Resources
Free Resources
Coding Platforms
Paid Courses
Tips for Success
Code Daily: Solve at least 1 problem/day.
Visualize Algorithms: Use Visualgo.
Learn Big O: Analyze time/space complexity.
Debug: Use
console.log()
or DevTools.
Example Problem: Palindrome Check
function isPalindrome(str) {
const cleaned = str.replace(/[^a-z0-9]/gi, '').toLowerCase();
return cleaned === cleaned.split('').reverse().join('');
}
console.log(isPalindrome("A man, a plan, a canal: Panama")); // true
Happy Coding! ๐ป
ย