Arrays & Hashing

Blind 75

Contains Duplicate

Question

Drawing Explanation

Code

class Solution {
    public boolean containsDuplicate(int[] nums) {
        Set<Integer> numbers = new HashSet<Integer>();
        for (int num : nums) {
            if (numbers.contains(num)) return true;
            numbers.add(num);
        }
        
        return false;
    }
}

Valid Anagram

Question

Drawing Explanation

Code

Two Sum

Question

Drawing Explanation

Code

public class Soluntion {
    public int[] twoSum(int[] nums, int target) {
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if (map.containsKey(nums[i])) {
                return new int[]{map.get(nums[i]), i};
            }
            map.put(target - nums[i], i);
        }
        return new int[0];
    }

Group Anagrams

Question

Drawing Explanation

Code

Top K Frequent Elements

Question

Drawing Explanation

Code

Product of Array Except Self

Question

Drawing Explanation

Code

public class Solution {
    public static int[] productExceptSelf(int[] nums) {
        int n = nums.length;
        int[] result = new int[n];
        Arrays.fill(result, 1);

        // Perform Prefix product operation by traversing Left -> Right
        int prefix = 1;
        for (int i = 0; i < nums.length; i++) {
            result[i] = prefix;
            prefix *= nums[i];
        }

        // Perform Postfix product operation by traversing Right -> Left
        int postfix = 1;
        for (int i = nums.length - 1; i >= 0; i--) {
            result[i] *= postfix;
            postfix *= nums[i];
        }
        return result;
    }
}

Encode and Decode Strings

Question

Drawing Explanation

Code

Longest Consecutive Sequences

Question

Drawing Explanation

Code

Last updated