Problem: Given an integer array nums
, return true
if any value appears at least twicein the array, and return false
if every element is distinct.
Solutions
Brute force
Just have two for loops and check for values.
- Time complexity:
- Space complexity:
Sorting
Sort the array; if there are any duplicated numbers in the list, return true.
- Time complexity: , sorting operation is bottleneck here
- Space complexity:
Hashset
Use a hashset to check; if a number is already in the set, return True
- Time complexity:
- Space complexity: