Duplicate Integer
Given an integer array nums, return true if any value appears more than once in the array, otherwise return false.
Example 1:
Input: nums = [1, 2, 3, 3]
Output: true
Example 2:
Input: nums = [1, 2, 3, 4]
Output: false
Solution
To find duplications, hash algorithm is easy to think of using a hash algorithm. In python, a set structure can be easily applied.
Code
class Solution:
def hasDuplicate(self, nums: List[int]) -> bool:
s = set()
for num in nums:
if num in s:
return True
s.add(num)
return False