8/41
Part 1 of 7 · SortingLearn and do
Sorted data puts related items next to each other
Sorting puts copies side by side.
items = [42, 7, 19, 7, 3]
s = sorted(items)
print(s)
print(any(a == b for a, b in zip(s, s[1:])))
[3, 7, 7, 19, 42]
True
zip(s, s[1:]) pairs each item with the one after it, so the check only looks at neighbors.
The hash-maps lesson's seen-set finds a repeat in O(n) time and O(n) memory. Sorting in place costs O(n log n) time but needs no set, only the sort's scratch space. That's the trade an interviewer asks about when memory is tight. Contains Duplicate is on Netflix's tag list (1 source).
Sorted data helps with pairs too. With one pointer at each end of a sorted list, one pass finds two values that add up to a target. The sliding-window lesson builds that.
In the editor, write smallest_gap(times): the two changes closest in time. It prints the gap for five times, then for one.