Write the function find_duplicates(L). Assume you are given a list L whose elements can all be compared with < and == — numbers, strings, or even lists, which cannot be dictionary keys or set members. find_duplicates returns a new list, in ascending order, holding every value that occurs two or more times in L, each such value listed exactly once. L itself must not change.
Checking every element against every other is . Instead, pay for one sort up front: once the values are in order, every group of equal values sits side by side, and a single pass finds them all.
print(find_duplicates([4, 1, 4, 2, 1, 4])) # [1, 4] print(find_duplicates(['b', 'a', 'c'])) # [] print(find_duplicates([[1, 2], [0], [1, 2]])) # [[1, 2]] L = [3, 3, 1] print(find_duplicates(L), L) # [3] [3, 3, 1]