How to uselist-dictionary approach (without any inbuild count function) in Python
Python3
def duplicate(input_list): new_dict, new_list = {}, [] for i in input_list: if not i in new_dict: new_dict[i] = 1 else : new_dict[i] + = 1 for key, values in new_dict.items(): if values > 1 : new_list.append(key) return new_list if __name__ = = '__main__' : input_list = [ 1 , 2 , 1 , 2 , 3 , 4 , 5 , 1 , 1 , 2 , 5 , 6 , 7 , 8 , 9 , 9 ] print (duplicate(input_list)) # This code is contributed by saikot |
Output
[1, 2, 5, 9]
Python | Program to print duplicates from a list of integers
Given a list of integers with duplicate elements in it. The task is to generate another list, which contains only the duplicate elements. In simple words, the new list should contain elements that appear as more than one.
Examples:
Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20] Output : output_list = [20, 30, -20, 60]
Input : list = [-1, 1, -1, 8] Output : output_list = [-1]