LeetCode题解:Contains Duplicate

来源:互联网 发布:淘宝网店代运营 编辑:程序博客网 时间:2024/06/05 06:44

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.

题意:给定一个整数数组,判断是否含有重复元素

解决思路:利用Set

代码:

public boolean containsDuplicate(int[] nums) {        Set<Integer> set = new HashSet<Integer>();         for(int i : nums)             if(!set.add(i))// if there is same                 return true;          return false;    }
0 0
原创粉丝点击