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.
Example 1:
Input: nums = [1,2,3,1]
Output: true
Explanation:
The element 1 occurs at the indices 0 and 3.
Example 2:
Input: nums = [1,2,3,4]
Output: false
Explanation:
All elements are distinct.
Example 3:
Input: nums = [1,1,1,3,3,4,3,2,4,2]
Output: true
Constraints:
1 <= nums.length <= 105-109 <= nums[i] <= 109When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you are holding a collection of items and need to figure out if any two of them are identical. The brute force approach involves exhaustively comparing every single item against every other item in the collection to see if any match.
Here's how the algorithm would work step-by-step:
def contains_duplicate_brute_force(collection_items):
# Iterate through every item to establish a baseline for comparison against the rest of the list
for current_index in range(len(collection_items)):
for comparison_index in range(current_index + 1, len(collection_items)):
# Compare the currently selected item against every subsequent item to detect any exact match
if collection_items[current_index] == collection_items[comparison_index]:
return True
# Exhausted all possible pairs without finding a match, so we conclude no duplicates exist
return FalseInstead of comparing every single item against all the others one by one, the optimal approach uses a special tracking tool like a personal checklist. As you review each item, you check off what you have seen so far, allowing you to instantly spot a repeat the moment you encounter it.
Here's how the algorithm would work step-by-step:
def contains_duplicate(collection_of_items):
# Initialize an empty checklist to track items we have already encountered.
personal_checklist = set()
for current_item in collection_of_items:
# Check if the item is already on our checklist to instantly spot duplicates.
if current_item in personal_checklist:
return True
# Add the item to our checklist so we remember seeing it for future checks.
personal_checklist.add(current_item)
# If we finish checking everything without matches, confirm items are unique.
return False| Case | How to Handle |
|---|---|
| Empty array or single element array | Return false immediately because duplicate values are impossible with fewer than two elements. |
| Array with exactly two elements, one duplicate and one unique | The hash set or sorting approach correctly identifies the matching pair and returns true. |
| Array with all identical elements | The second element encountered will already exist in the set, triggering an immediate true return. |
| Array containing negative numbers and zeros alongside positives | Hash sets and sorting algorithms handle negative numbers and zeros seamlessly without any special offset logic. |
| Extremely large arrays with maximum constraints | A hash set approach ensures linear time complexity of O(n) to prevent time limit exceedance. |
| Extremely large arrays with memory constraints | A sorting approach can be used instead of a hash set to achieve O(1) extra space complexity at the cost of O(n log n) time. |
| Inputs containing extreme boundary integer values | Standard hash set implementations natively support full integer range keys without overflow or collision issues. |