Data Structures & Algorithms View on GitHub โ†—

Duplicate Integer

Java

class Solution {
    public boolean hasDuplicate(int[] nums) {
        
        Set<Integer> set = new HashSet<>();

        for(int i : nums){

            if(set.contains(i)) return true;

            set.add(i);
        }

        return false;
    }
}

Python

class Solution:
    def hasDuplicate(self, nums: List[int]) -> bool:
        hashSet = set()

        for num in nums:
            if num in hashSet:
                return True
            hashSet.add(num)

        return False

Enjoyed this solution?

I write about software engineering, algorithms, and lessons learned. Check out more in the newsletter.

Browse the newsletter