Reference: LeetCode
Difficulty: Easy
Problem
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 returnfalse
if every element is distinct.
Example:
1 | Input: [1,2,3,1] |
Analysis
Brute-Force
Skip.
Time: $O(N^2)$
Space: $O(1)$
Hash Set
1 | public boolean containsDuplicate(int[] nums) { |
Time: $O(N)$
Space: $O(N)$
Sorting
1 | public boolean containsDuplicate(int[] nums) { |
Time: $O(N\log{N})$
Space: $O(1)$