Given an integer array, determine whether there are duplicate elements. The function returns true if any value appears at least twice in the array. Returns false if every element in the array is different.
示例 1: 输入: [1,2,3,4] 输出: true
Method 1: Sorting time complexity is O (NlogN) space complexity is O (logN)
nums.sort() for i in range(len(nums)-1): #判断前一个与后一个数是否相等 if nums[i] == nums[i+1]: return True return False
Method 2: Hash table
dic = {} for i in range(len(nums)): if nums[i] in dic: return True else: dic[nums[i]] = 1 return False
The above is the detailed content of How to determine if there are duplicate elements in Java. For more information, please follow other related articles on the PHP Chinese website!