Skip to main content

DSA in JavaScript: Learn Data Structures and Algorithms with Code Examples

Data Structures and Algorithms (DSA) in JavaScript



Data Structures and Algorithms (DSA) form the backbone of efficient programming and problem-solving. Whether you're preparing for coding interviews or improving your coding skills, understanding DSA in JavaScript is crucial. This guide will cover fundamental data structures and algorithms, their implementations, and real-world use cases.


1. Understanding Data Structures

data structure is a way of organizing and storing data to perform operations efficiently.

Types of Data Structures:

  1. Linear Data Structures (Elements are arranged sequentially)

    • Arrays

    • Linked Lists

    • Stacks

    • Queues

  2. Non-Linear Data Structures (Elements are not arranged sequentially)

    • Trees

    • Graphs

  3. Hash-based Data Structures

    • Hash Tables (Objects, Maps in JavaScript)


2. Arrays in JavaScript

An array is a collection of elements stored in contiguous memory locations. JavaScript provides built-in array methods for manipulation.

Operations on Arrays

javascript
let arr = [10, 20, 30, 40, 50]; 
// Accessing Elements console.log(arr[2]);
// Output: 30 // Adding an Element at the End arr.push(60); console.log(arr);
// [10, 20, 30, 40, 50, 60] 
// Removing an Element from the End arr.pop();
console.log(arr); // [10, 20, 30, 40, 50] // Adding an Element at the Beginning arr.unshift(0); console.log(arr); // [0, 10, 20, 30, 40, 50] // Removing an Element from the Beginning 
arr.shift(); console.log(arr); // [10, 20, 30, 40, 50] // Searching for an Element console.log(arr.indexOf(30)); // Output: 2 // Slicing an Array console.log(arr.slice(1, 4)); // Output: [20, 30, 40]

Time Complexity:

  • Access: O(1)

  • Search: O(n) (Linear Search)

  • Insert/Delete: O(n) (Shifting elements)


3. Linked List in JavaScript

linked list consists of nodes where each node contains a value and a pointer to the next node.

Types of Linked Lists

  • Singly Linked List (Each node points to the next)

  • Doubly Linked List (Each node has both next and previous pointers)

  • Circular Linked List (Last node points to the first node)

Implementation of Singly Linked List

javascript
class Node {
constructor(value) {
this.value = value;
this.next = null; } 
class LinkedList {
constructor() { this.head = null
 } 
append(value) {
let newNode = new Node(value);
if (!this.head) {
this.head = newNode; return
 } let temp = this.head;
while (temp.next) { temp = temp.next;
 }
 temp.next = newNode; 
 }
printList() {
let temp = this.head
while (temp) {
console.log(temp.value);
 temp = temp.next;
 } } } // Usage let list = new LinkedList(); 
list.append(10); 
list.append(20); list.append(30);
list.printList(); // Output: 10 20 30

Time Complexity:

  • Access: O(n)

  • Insert at End: O(n)

  • Insert at Beginning: O(1)

  • Deletion: O(n)


4. Stacks in JavaScript

stack follows the LIFO (Last In, First Out) principle.

Operations on Stack

  • push(value) → Add element to the top

  • pop() → Remove element from the top

  • peek() → Get the top element

  • isEmpty() → Check if stack is empty

Stack Implementation in JavaScript

javascript
class Stack {
constructor() { this.items = [];
 } 
push(value) { 
this.items.push(value); 
 } 
pop() { return this.items.pop();
 } 
peek() { return this.items[this.items.length - 1];
 } isEmpty() { return this.items.length === 0
 } } // Usage let stack = new Stack();
 stack.push(10);
 stack.push(20);
console.log(stack.peek());
// Output: 20 
stack.pop();
console.log(stack.peek()); 
// Output: 10

5. Queues in JavaScript

queue follows the FIFO (First In, First Out) principle.

Queue Implementation in JavaScript

javascript
class Queue
constructor() { this.items = [];
 } 
enqueue(value) {
this.items.push(value); 
 } 
dequeue() { return this.items.shift();
 } 
front() { return this.items[0]; }
 } 
// Usage let queue = new Queue();
 queue.enqueue(10); queue.enqueue(20);
console.log(queue.front()); // Output: 10 queue.dequeue(); console.log(queue.front()); // Output: 20

6. Sorting Algorithms in JavaScript

Sorting algorithms arrange elements in a specific order.

Bubble Sort

javascript
function bubbleSort(arr) {
let n = arr.length
for (let i = 0; i < n - 1; i++) {
for (let j = 0; j < n - 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, 2, 9, 1, 5, 6])); // Output: [1, 2, 5, 5, 6, 9]

Quick Sort

javascript
function quickSort(arr) { 
if (arr.length <= 1) return arr;
let pivot = arr[arr.length - 1];
let left = arr.filter((el) => el < pivot);
let right = arr.filter((el) => el > pivot);
return [...quickSort(left), pivot, ...quickSort(right)];
 }
console.log(quickSort([5, 2, 9, 1, 5, 6])); // Output: [1, 2, 5, 5, 6, 9]

7. Searching Algorithms in JavaScript

Searching algorithms help find elements in a dataset.

Linear Search

javascript
function linearSearch(arr, target) {
for (let i = 0; i < arr.length; i++) {
if (arr[i] === target) return i;
 } 
return -1; }

Binary Search (Only for Sorted Arrays)

javascript
function binarySearch(arr, target) {
let left = 0, right = arr.length - 1;
while (left <= right) { 
let mid = Math.floor((left + right) / 2);
if (arr[mid] === target)
return mid; 
else if (arr[mid] < target) left = mid + 1;
else right = mid - 1;
 } 
return -1; }

Conclusion

Mastering Data Structures and Algorithms in JavaScript is essential for writing efficient and scalable applications. By practicing these concepts, you'll be well-prepared for coding interviews and real-world problem-solving.