Computer Science, asked by swamikhushi123, 5 hours ago

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 anindyaadhikari13
2

\texttt{\textsf{\large{\underline{Solution}:}}}

The given co‎de 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=', ')

\texttt{\textsf{\large{\underline{Explanation}:}}}

  • 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() fu‎nction returns 1.
  • So, if count() fu‎nction 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