217. Contains Duplicate

    xiaoxiao2021-03-25  127

    

    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.

    public class Solution { public boolean containsDuplicate(int[] nums) { HashSet<Integer> a=new HashSet<>(); if(nums==null||nums.length==0) return false; for(int i=0;i<nums.length;i++) { if(!a.add(nums[i])) return true; else a.add(nums[i]); } return false; } } HashSet<Integer> a=new HashSet<>();                  if(nums==null||nums.length==0)                      return false;                  for(int i=0;i<nums.length;i++)                  {                                         a.add(nums[i]);                  }                  if(a.size()==nums.length)                   return false;                  else                   return true;

    转载请注明原文地址: https://ju.6miu.com/read-14252.html

    最新回复(0)