存在重复元素

题目:

 

 

解:Arrays.sort(nums);set集合

    public boolean containsDuplicate(int[] nums) {
       Set<Integer> set = new HashSet<>();
       for (int num : nums) {
           //因为集合set中不能有重复的元素,如果有重复的
           //元素添加,就会添加失败
           if (!set.add(num))
               return true;
      }
       return false;
  }
    public boolean containsDuplicate(int[] nums) {
       Set<Integer> set = new HashSet<>();
       for (int num : nums) {
           //因为集合set中不能有重复的元素,如果有重复的
           //元素添加,就会添加失败
           if (!set.add(num))
               return true;
      }
       return false;
  }
 
posted @ 2022-04-22 09:04  仙人掌掌掌掌  阅读(26)  评论(0)    收藏  举报