美文网首页
217. Contains Duplicate(HashTabl

217. Contains Duplicate(HashTabl

作者: liuhaohaohao | 来源:发表于2018-04-07 13:38 被阅读0次

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.

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

相关文章

网友评论

      本文标题:217. Contains Duplicate(HashTabl

      本文链接:https://www.haomeiwen.com/subject/gnhyhftx.html