Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Let me show you some practical everyday algorithms that solve common problems.

  1. Finding the Most Frequent Element
def find_most_frequent(arr):
    counter = {}
    for item in arr:
        counter[item] = counter.get(item, 0) + 1
    
    max_count = 0
    most_frequent = None
    for item, count in counter.items():
        if count > max_count:
            max_count = count
            most_frequent = item
    return most_frequent

# Example
numbers = [1, 2, 3, 2, 4, 2, 5, 3]
result = find_most_frequent(numbers)  # Returns 2
  1. Palindrome Checker
def is_palindrome(text):
    # Remove non-alphanumeric characters and convert to lowercase
    cleaned_text = ''.join(c.lower() for c in text if c.isalnum())
    return cleaned_text == cleaned_text[::-1]

# Examples
print(is_palindrome("A man, a plan, a canal: Panama"))  # True
print(is_palindrome("race a car"))  # False
  1. Finding Missing Number in Sequence
def find_missing_number(arr):
    n = len(arr) + 1  # Expected length
    expected_sum = (n * (n + 1)) // 2
    actual_sum = sum(arr)
    return expected_sum - actual_sum

# Example
numbers = [1, 2, 4, 6, 3, 7, 8]
missing = find_missing_number(numbers)  # Returns 5
  1. String Pattern Matching (Simple)
def find_pattern_occurrences(text, pattern):
    positions = []
    n, m = len(text), len(pattern)
    
    for i in range(n - m + 1):
        if text[i:i+m] == pattern:
            positions.append(i)
    
    return positions

# Example
text = "AABAACAADAABAAABAA"
pattern = "AA"
positions = find_pattern_occurrences(text, pattern)  # Returns [0, 3, 9, 12, 13, 16]
  1. Finding Pairs that Sum to Target
def find_pairs(arr, target):
    seen = set()
    pairs = []
    
    for num in arr:
        complement = target - num
        if complement in seen:
            pairs.append((complement, num))
        seen.add(num)
    
    return pairs

# Example
numbers = [2, 7, 11, 15, 3, 6]
target_sum = 9
result = find_pairs(numbers, target_sum)  # Returns [(2, 7), (3, 6)]

These algorithms demonstrate different problem-solving strategies:

  • Hash tables for counting and lookup (find_most_frequent, find_pairs)
  • String manipulation (is_palindrome)
  • Mathematical formulas (find_missing_number)
  • Simple pattern matching (find_pattern_occurrences)

Would you like me to explain any of these in more detail or show you some variations of these algorithms?