In: Computer Science
In the Python:
Note 1: You may not use these python built-in functions:
sorted(), min(), max(), sum(), pow(), zip(), map(), append(), count() and counter().
(7 points) unique.py: A number in a list is unique if it appears only once. Given a list of
random numbers, print the unique numbers and their count. Print the duplicate numbers and
their count.
Sample runs:
Enter the size of the list : 7
[8, 2, 6, 5, 2, 4, 5]
There are 3 unique numbers: 8 6 4
There are 2 duplicate numbers: 2 5
Sample run:
Enter the size of the list : 7
[2, 2, 4, 3, 3, 3, 4]
There are 0 unique numbers:
There are 3 duplicate numbers: 2 3 4
CODE
n = int(input("Enter the size of the list : "))
dict = {}
for i in range(n):
el = int(input("Enter the element " + str(i+1) + " : "))
if el in dict:
dict[el] = dict[el] + 1;
else:
dict[el] = 1
unique = 0
duplicate = 0
uniqueNumbers = ""
duplicateNumbers = ""
for key in dict:
if (dict[key] == 1):
unique += 1
uniqueNumbers += str(key) + " "
elif dict[key] > 1:
duplicate += 1
duplicateNumbers += str(key) + " "
print("There are %d unique numbers: %s" %(unique, uniqueNumbers))
print("There are %d duplicate numbers: %s" %(duplicate, duplicateNumbers))