· 1 min read

Blind 75 - Contains Duplicate

Create a set and put items in one by one while checking if `num` is already there.

Link

Video

https://youtu.be/7KHKFqLPMbs

Problem and Constraints

If the array contains any duplicate, return true. Else false.

All Approaches and Explanations in English

O(n^2) solution. O(1) space complexity

Double loop. And check if the elements are equals.

Bad. Solution.

O(n) time complexity. O(n) space complexity

Create a set and put items in one by one while checking if num is already there.

Code, if any

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

Back to Blog