Pages

Sunday, November 30, 2025

Contains duplicate - using HashSet (optimized code)

 class Solution {

    public boolean containsDuplicate(int[] nums) {
        HashSet<Integer> seen = new HashSet<>();

        for(int i=0; i<nums.length; i++) {
            if(seen.contains(nums[i])) { // methods: add(), contains(), remove()
                return true;
            } else {
                seen.add(nums[i]);
            }
        }
        return false;
    }
}

No comments:

Post a Comment

3917. Count Indices With Opposite Parity (Brute Force) O(n2) + Optimized Solution O(n) + tips LEETCODE WEEKLY 500

  class Solution {     public int [] countOppositeParity ( int [] nums ) {          // approach 1 - checks every pair         int n = n...