Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
2 Annotated Answers solution in Clear category for Non-unique Elements by BootzenKatzen
from collections.abc import Iterable
# First solution:
def checkio(data: list[int]) -> Iterable[int]:
nonunique = [] # Creating a new list to add non-unique values to
for item in data: # iterating through each item of the input list
if data.count(item) > 1: # if the count of that item is >1 (not unique)
nonunique.append(item) # add that item to our new list
return nonunique # when we've gone through all the items return the new list
# "Generator" Solution:
def checkio2(data: list[int]) -> Iterable[int]:
return (a for a in data if data.count(a) > 1)
# This is basically the same logic as the first answer, just fit into one line
# The a for a in data is the iterator like "for item in data"
# except it only will return a if it meets the following conditions
# And those conditions: if data count(a) > 1 is the same as above
# So it will return a if the count of a > 1
# and skip values where that condition is false
# So it basically iterates over the values and checks if they're unique or not in one step
# Then returns the final list
print("Example:")
print(list(checkio([1, 2, 3, 1, 3])))
# These "asserts" are used for self-checking
assert list(checkio([1, 2, 3, 1, 3])) == [1, 3, 1, 3]
assert list(checkio([1, 2, 3, 4, 5])) == []
assert list(checkio([5, 5, 5, 5, 5])) == [5, 5, 5, 5, 5]
assert list(checkio([10, 9, 10, 10, 9, 8])) == [10, 9, 10, 10, 9]
assert list(checkio([2, 2, 3, 2, 2])) == [2, 2, 2, 2]
assert list(checkio([10, 20, 30, 10])) == [10, 10]
assert list(checkio([7])) == []
assert list(checkio([0, 1, 2, 3, 4, 0, 1, 2, 4])) == [0, 1, 2, 4, 0, 1, 2, 4]
assert list(checkio([99, 98, 99])) == [99, 99]
assert list(checkio([0, 0, 0, 1, 1, 100])) == [0, 0, 0, 1, 1]
assert list(checkio([0, 0, 0, -1, -1, 100])) == [0, 0, 0, -1, -1]
print("The mission is done! Click 'Check Solution' to earn rewards!")
Sept. 13, 2023