Math Fundamentals18 sections · 814 units
Open in Course

Contains Duplicate - Insight

Three approaches

Approach 1 (Brute Force): Compare every pair. Time: O(n2)O(n^2), Space: O(1)O(1).

Approach 2 (Sorting): Sort then check adjacent elements. Time: O(nlogn)O(n \log n), Space: O(1)O(1) or O(n)O(n) depending on sort.

Approach 3 (Hash Set): Add elements to a set, check for duplicates. Time: O(n)O(n), Space: O(n)O(n). The hash set trades space for better time.