217 Contains Duplicate – Easy


Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.


This is too straight forward and simple. I am curious if there is a solution that only takes O(1) space.

From running time perspective, we have to touch every element at lease once, so it needs to be O(n).


public class Solution {
    public boolean containsDuplicate(int[] nums) {
        HashSet<Integer> set = new HashSet<Integer>();
        for (int i = 0; i < nums.length; i ++){
            if (set.contains(nums[i])){
                return true;
        return false;

results matching ""

    No results matching ""