[LeetCode]217. Contains Duplicate

来源:互联网 发布:凯酷 mac 编辑:程序博客网 时间:2024/06/05 20:59

题目描述: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.
分析:给一串整数,找出这一串整数中是否包含任何重复的数。当任何值出现两次以上则返回true,当有值只出现一次则返回false

public class Solution {    public boolean containsDuplicate(int[] nums) {        //use a data structer hash table to identify weather an element has been previously encountered in the array        Set<Integer> set=new HashSet<Integer>();        for(int i:nums){            if(set.contains(i)){                return true;            }            set.add(i);        }        return false;    }}
原创粉丝点击