Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Non-unique Elements by treyhunner
#Your optional code here
#You can import some modules or create additional functions
from collections import defaultdict
def checkio(data):
times_seen = defaultdict(int)
for num in data:
times_seen[num] += 1
non_unique = {num for (num, times) in times_seen.items() if times > 1}
return [num for num in data if num in non_unique]
#Some hints
#You can use list.count(element) method for counting.
#Create new list with non-unique elements
#Loop over original list
if __name__ == "__main__":
#These "asserts" using only for self-checking and not necessary for auto-testing
assert isinstance(checkio([1]), list), "The result must be a list"
assert checkio([1, 2, 3, 1, 3]) == [1, 3, 1, 3], "1st example"
assert checkio([1, 2, 3, 4, 5]) == [], "2nd example"
assert checkio([5, 5, 5, 5, 5]) == [5, 5, 5, 5, 5], "3rd example"
assert checkio([10, 9, 10, 10, 9, 8]) == [10, 9, 10, 10, 9], "4th example"
Aug. 16, 2015