Given an integer array, print all distinct elements in array. The given array may contain duplicates and the output should print every element only once. The given array is not sorted.
Examples:
Input: arr[] = {12, 10, 9, 45, 2, 10, 10, 45} Output: 12, 10, 9, 45, 2 Input: arr[] = {1, 2, 3, 4, 5} Output: 1, 2, 3, 4, 5 Input: arr[] = {1, 1, 1, 1, 1} Output: 1
This problem has existing solution please refer Print All Distinct Elements of a given integer array link. We will solve this problem in python quickly using Ordered Dictionary. Approach is simple,
- Convert array into dictionary data structure using OrderedDict.fromkeys(iterable) function, it converts any iterable into dictionary having elements as Key in the same order they appeared in array.
- Now iterate through complete dictionary and print keys.
# Python program to print All Distinct # Elements of a given integer array from collections import OrderedDict def printDistinct( input ): # convert list into ordered dictionary ordDict = OrderedDict.fromkeys( input ) # iterate through dictionary and get list of keys # list of keys will be resultant distinct elements # in array result = [ key for (key, value) in ordDict.items() ] # concatenate list of elements with ', ' and print print ( ', ' .join( map ( str , result))) # Driver program if __name__ = = "__main__" : input = [ 12 , 10 , 9 , 45 , 2 , 10 , 10 , 45 ] printDistinct( input ) |
Output:
12, 10, 9, 45, 2
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.