Updated 2022-05-22 3
Viewed 8 times

given a list of integers nums, return a list of all the element but the repeating number should not be printed more than twice


input: nums = [1,1,2,3,3,4,4,4,5]

output: [1,1,2,3,3,4,4,5]

🟢 Solution

The easiest and I guess most straight forward way to use unique collections is with a set:

list(set(nums)) -> [1, 2, 3, 4, 5]

The downside of this approuch is that sets are unordered. And we cannot really depend on how the list will be sorted after the conversion.

If order is important in your case you can do this:

[1, 2, 3, 4, 5]

dicts are ordered since python3 came out, and their keys are unique. So with this small trick we get a list of the unique keys of a dictionary, but still maitain the original order!