Why a hash set finds a repeat in one pass, and what sorting first buys you.
About this comparison
Three approaches race on the same seven numbers, 42, 17, 88, 5, 63, 29 and 29, and ask whether any number appears twice. Each one does one comparison or one lookup per beat, and its counter keeps the score, so the totals at the end are the work each one needed.
Brute force compares every pair with two loops: 42 with each number after it, then 17, and so on. It needs no extra memory, but the pairs grow with the square of n, O(n²).
Sort and scan sorts the row first: 5, 17, 29, 29, 42, 63, 88. After a sort, equal numbers sit side by side, so one walk that compares each number with the next one finds any repeat. The sort is O(n log n) and the walk is O(n).
The hash set reads the numbers once. For each one it asks whether that number is already stored, and stores it if not. A lookup is O(1) on average, so the pass is O(n), paid for with O(n) memory.
Brute force spends time, the hash set spends memory, and sort and scan spends a sort. Sorting in place also reorders the input, which the caller may not want; the hash set leaves it as it was.
The repeat is the last two numbers, 29 and 29: brute force's worst case, all 21 pairs. The hash set looks 7 times and finds 29 already stored on the last one. Sort and scan finishes first: 5 and 17 differ, 17 and 29 differ, then 29 and 29 match.
The sort is drawn as one beat, which hides real work: cheap on seven numbers, but on a large input the sort outgrows the hash set's single pass.
Use the hash set for Contains Duplicate as it is usually asked: one pass, stop at the first repeat. Use sort and scan when memory is tight and the array may be reordered: O(1) extra space after an in-place sort. Brute force is fine for a handful of numbers, and as a first draft to test the others against.