non unique elements list | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

non unique elements list

#********************************** ## NonuniqueElements.py ## Created: Fares Younis ## Change history: ## 22.05.2017 # FARES YOUNIS #********************************** #return a list consisting of only the non-unique elements in this list. #To do so you will need to remove all unique elements (elements which are contained in a given list only once). https://code.sololearn.com/ckELfU9UCjHJ/?ref=app

25th May 2017, 7:40 AM
Fares Younis
Fares Younis - avatar
1 Answer
+ 1
Looks good, nice comments. Just wanted to point out that the complexity of your method is O(n^2) because List.count() is O(n) and you're calling it in a while loop. You can bring it down to O(n) by using a counting dictionary - def nonunique(listX): print('Number of Element in this list:' + str(len(listX))) countingDictionary = dict() for i in listX: countingDictionary [i] = countingDictionary.get(i, 0) + 1 for key in countingDictionary.keys(): if countingDictionary[key] <= 1: listX.remove(key) return listX
3rd Jun 2017, 9:47 PM
Tom Cox
Tom Cox - avatar