Skip to content

Latest commit

 

History

History
29 lines (22 loc) · 509 Bytes

Contains Duplicate.md

File metadata and controls

29 lines (22 loc) · 509 Bytes

Hint

Use a HashSet to detect duplicates.

Solution

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

Time/Space Complexity

  • Time Complexity: O(n)
  • Space Complexity: O(n)

Links