Programming

Essential Algorithms for Developers

12 ديسمبر 202512 min read
Essential Algorithms for Developers

Core algorithms and data structures every developer should know.

Why Learn Algorithms?

Algorithms improve problem-solving skills. Essential for interviews and optimizing code performance.

Big O Notation

O(1)      - Constant   - Array access
O(log n)  - Logarithmic - Binary Search
O(n)      - Linear     - Loop through array
O(n log n)- Linearithmic - Merge Sort
O(n²)     - Quadratic  - Bubble Sort

Binary Search

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

Two Pointers

// Pair that equals target sum?
function twoSum(arr, target) {
  let left = 0;
  let right = arr.length - 1;
  
  while (left < right) {
    const sum = arr[left] + arr[right];
    if (sum === target) return [left, right];
    if (sum < target) left++;
    else right--;
  }
  return null;
}

Hash Map for Duplicates

function findDuplicate(arr) {
  const seen = new Set();
  for (const num of arr) {
    if (seen.has(num)) return num;
    seen.add(num);
  }
  return null;
}

Conclusion

Learn core algorithms and practice on LeetCode. Consistent practice is the key to mastery.

Tags

#Algorithms#Data Structures#Coding#Interviews#Basics

Related Posts