0217 Contains Duplicate
Solved at: 2023-01-29
Question
Given an integer array nums
, return true
if any value appears at least twice in the array, and return false
if every element is distinct.
Solution
class Solution {
func containsDuplicate(_ nums: [Int]) -> Bool {
var set = Set<Int>()
for num in nums {
if set.contains(num) {
return true
}
else {
set.insert(num)
}
}
return false
}
}
Results
- Runtime 642 ms Beats 44.57%
- Memory 19 MB Beats 10.85%
Complexity Analysis
Both
Takeaways
Using sorting can sacrifice time to and pull down space into