Here we are trying to print all the duplicate numbers from a list of numbers. So, we are trying to print all the numbers which occur more than once in a list (not unique in the list).
Examples
Input: given_list = [ 3, 6, 9, 12, 3, 30, 15, 9, 45, 36, 12] Output: desired_output = [3, 9, 12] Input: given_list = [-27, 4, 29, -27, -2 , -99, 123, 499, -99] Output: desired_output = [-27, -99]
Below is the code to find the duplicates elements from a given list −
lst = [ 3, 6, 9, 12, 3, 30, 15, 9, 45, 36, 12, 12] dupItems = [] uniqItems = {} for x in lst: if x not in uniqItems: uniqItems[x] = 1 else: if uniqItems[x] == 1: dupItems.append(x) uniqItems[x] += 1 print(dupItems)
Output
[3, 9, 12]
Above program will work not only for a list of integers but others too −
Input: given_list = ['abc','def','raj','zack','abc','raj'] Output: output_returned= ['abc', 'raj']