给定一个整数数组,判断是否存在重复元素。
如果存在一值在数组中出现至少两次,函数返回 true
。如果数组中每个元素都不相同,则返回 false
。
作者:力扣 (LeetCode)
链接:https://leetcode-cn.com/leetbook/read/top-interview-questions-easy/x248f5/
来源:力扣(LeetCode)
本题可以使用set集合不可重复的特性来解决
public class Demo_4 {
public boolean containsDuplicate(int[] nums) {
HashSet<Integer> set = new HashSet<>();
for (int i = 0; i < nums.length; i++) {
set.add(nums[i]);
}
return set.size()!=nums.length;
}
}
本系列为个人学习使用,同时也欢迎大家一起学习讨论