Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python program to print all distinct elements of a given integer array.
The distinct elements are the values that appear only once or uniquely in an array. When working with arrays, we often encounter repeated or duplicate values. In this article, we will explore different methods to print all distinct elements of a given integer array.
Identifying and printing these distinct elements is a common task to avoid unexpected results. This can be achieved using Python built-in tools like sets, dictionaries, and loops.
Using Python set() Function
In this approach, we use the Python set() function, which automatically removes all duplicate values from the list because a set only stores unique values.
We pass the list as an argument to the set() function to filter out duplicates and return a set with distinct elements.
Syntax
set(iterable)
Example
Let's look at the following example, where we print all distinct elements from the array [1,2,3,2,4,1] ?
array = [1, 2, 3, 2, 4, 1]
result = set(array)
print("Distinct elements using set():", result)
Distinct elements using set(): {1, 2, 3, 4}
Using Loop and List
In this approach, we initialize an empty list and iterate over each element in the original array. During iteration, we check whether the element is not already in the empty list and then add it. This preserves only unique elements while maintaining insertion order.
Example
In the following example, we print distinct elements from the array [11,22,11,33,22] by manually checking for duplicates using a loop ?
array = [11, 22, 11, 33, 22]
distinct_elements = []
for element in array:
if element not in distinct_elements:
distinct_elements.append(element)
print("Distinct elements using loop:", distinct_elements)
Distinct elements using loop: [11, 22, 33]
Using Python dict.fromkeys() Method
The Python dict.fromkeys() method creates a new dictionary from the given iterable as keys with values provided by the user. This method preserves insertion order while removing duplicates.
We pass the array to dict.fromkeys() to create a dictionary, then convert the keys back to a list using list().
Example
Consider the following example, where we remove duplicates from the array [1,22,1,3,1] using the dict.fromkeys() method ?
array = [1, 22, 1, 3, 1]
result = list(dict.fromkeys(array))
print("Distinct elements using dict.fromkeys():", result)
Distinct elements using dict.fromkeys(): [1, 22, 3]
Comparison of Methods
| Method | Preserves Order? | Time Complexity | Best For |
|---|---|---|---|
set() |
No | O(n) | When order doesn't matter |
| Loop + List | Yes | O(n²) | Small arrays, educational purposes |
dict.fromkeys() |
Yes | O(n) | Order preservation with efficiency |
Conclusion
Use set() for fastest duplicate removal when order doesn't matter. Use dict.fromkeys() when you need to preserve insertion order efficiently. The loop method helps understand the underlying logic but is less efficient for large datasets.
