Home > Mobile >  how to check if only the selected element has a duplicate in array
how to check if only the selected element has a duplicate in array

Time:10-14

from collections import Counter

selected_element = 3
arr = [1,2,3,4,3,5,5]


def duplicates(values):
    dups = Counter(values) - Counter(set(values))
    return list(dups.keys())
print(duplicates(arr))


output: [3, 5]

i want only the selected element to be displayed which is '3' how would I do that?

CodePudding user response:

You can easily use count :

selected_element = 3
arr = [1,2,3,4,3,5,5]
print(arr.count(selected_element)>1)

count of selected element should be greater than 1 if it has any duplicates.

CodePudding user response:

Check whether selected_element is in dups.

print(selected_element in duplicates(arr))
  • Related