Core Concepts

graph LR A["Key: 'apple'"] -->|Hash Function| B["Index 2"] C["Key: 'banana'"] -->|Hash Function| D["Index 5"] B -.-> E[("Value: 12")] D -.-> F[("Value: 7")]

Cheatsheet Formulas

Classic Problem: Contains Duplicate

Given an integer array nums, return true if any value appears at least twice in the array.

class Solution {
    public boolean containsDuplicate(int[] nums) {
        Set<Integer> set = new HashSet<>();
        for (int num : nums) {
            if (!set.add(num)) {
                return true;
            }
        }
        return false;
    }
}
class Solution:
    def containsDuplicate(self, nums: List[int]) -> bool:
        return len(set(nums)) != len(nums)