CS Fundamentals Trainer · Algorithms & Data Structures · JavaScript
What is the time complexity of this code?
function hasDuplicate(arr) {
for (let i = 0; i < arr.length; i++) {
for (let j = i + 1; j < arr.length; j++) {
if (arr[i] === arr[j]) return true;
}
}
return false;
}
- O(n)
- O(n log n)
- O(n²)
- O(n³)
Difficulty: medium