Write a python program to display unique and duplicate items of a given list.[2,7,1,4,9,5,1,4,3].
Answers
Answered by
2
The given code is written in Python.
list_given=[2,7,1,4,9,5,1,4,3]
unique_elements=list()
duplicate_elements=set()
for element in list_given:
if list_given.count(element)==1:
unique_elements.append(element)
else:
duplicate_elements.add(element)
print('Unique Elements from the list are as follows -')
print(*unique_elements,sep=', ')
print('Duplicate Elements from the list are as follows -')
print(*duplicate_elements,sep=', ')
- Here, we have a list given with some numbers. We have declared another list that stores unique elements and a set that stores duplicate elements.
- The count() method of list counts the number of times an element is present in a list. If the element is unique, the count() function returns 1.
- So, if count() function returns 1 for element, then the number is added in the unique list or else, the number is added in the set. At last, the elements are displayed.
See the attachment for output.
Attachments:
Similar questions