Skip to content

Example: Contains Duplicate

Roberto Fronteddu edited this page Jul 25, 2024 · 3 revisions

Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.

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;
    }
}
Clone this wiki locally