LeetCode 217
Contains Duplicate
Concepts:
Hashmap
Set
Description: 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.
Brute-force Approach
Iterate through the array and for each element, iterate through the rest of the array to check for duplicates.
Time: O(n^2)
Space: O(1)
Better Approach
Sort the array and iterate through it, checking for duplicate neighbors.
Time: O(nlogn)
Space: O(1)
Optimal Approach
Iterate through the array and add the elements to a hash set. If an element already exists, return true. Can also be done using a hashmap.
Time: O(n)
Space: O(n)