AlgoViz
← All problems

Contains Duplicate

Easy+60 XPteaches: Bubble Sort

Given an integer array `nums`, return `true` if any value appears at least twice, and `false` if every element is distinct.

Example: nums = [1, 5, 3, 5, 2] → true

Brute force: Compare every pair of elements. (time O(n²), space O(1))

No visualization loaded.

Watch

i

Press Run to begin.

0/0

Why the best approach wins

The brute-force pair check does O(n²) work re-scanning the array. A hash set answers “have I seen this before?” in O(1), so a single O(n) pass suffices — at the cost of O(n) memory.

Brute force: O(n²) time / O(1) spaceHash set (one pass): O(n) time / O(n) space

Your turn — implement containsDuplicate

Loading editor…