Before coding: identify why big o & complexity works here.
Why algorithm efficiency matters. Time vs space tradeoffs and how to read Big-O notation.
Problem
You're given a list of integers. Determine whether any number appears more than once. Return `true` if a duplicate exists, or `false` if every number is unique.
Examples
Example 1
Input: nums = [1,2,3,1]
Output: true
Example 2
Input: nums = [1,2,3,4]
Output: false
Example 3
Input: nums = [1,1,1,3,3,4,3,2,4,2]
Output: true
⚡Focus on reducing time complexity from the naive solution.
TypeScript⚡
JavaScript
JavaPro
PythonPro
GoPro
loading compiler…
Loading editor…
🧪 Test cases3 tests · not run
▾
Test 1
nums = [1,2,3,1]
Expected: true
Test 2
nums = [1,2,3,4]
Expected: false
Test 3
nums = [1,1,1,3,3,4,3,2,4,2]
Expected: true
Target complexity for the optimal solution to this problem.
⏱ Time complexity
O(n)
Single pass — O(1) average lookup/insert per element in a hash set.