# Print All Distinct Elements of a given integer array

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```

A Simple Solution is to use twp nested loops. The outer loop picks an element one by one starting from the leftmost element. The inner loop checks if the element is present on left side of it. If present, then ignores the element, else prints the element. Following is the implementation of the simple algorithm.

## C++

 `// C++ program to print all distinct elements in a given array ` `#include ` `#include ` `using` `namespace` `std; ` ` `  `void` `printDistinct(``int` `arr[], ``int` `n) ` `{ ` `    ``// Pick all elements one by one ` `    ``for` `(``int` `i=0; i

## Java

 `// Java program to print all distinct ` `// elements in a given array ` `import` `java.io.*; ` ` `  `class` `GFG { ` ` `  `    ``static` `void` `printDistinct(``int` `arr[], ``int` `n) ` `    ``{ ` `        ``// Pick all elements one by one ` `        ``for` `(``int` `i = ``0``; i < n; i++) ` `        ``{ ` `            ``// Check if the picked element  ` `            ``// is already printed ` `            ``int` `j; ` `            ``for` `(j = ``0``; j < i; j++) ` `            ``if` `(arr[i] == arr[j]) ` `                ``break``; ` `     `  `            ``// If not printed earlier,  ` `            ``// then print it ` `            ``if` `(i == j) ` `            ``System.out.print( arr[i] + ``" "``); ` `        ``} ` `    ``} ` `     `  `    ``// Driver program ` `    ``public` `static` `void` `main (String[] args)  ` `    ``{ ` `        ``int` `arr[] = {``6``, ``10``, ``5``, ``4``, ``9``, ``120``, ``4``, ``6``, ``10``}; ` `        ``int` `n = arr.length; ` `        ``printDistinct(arr, n); ` ` `  `    ``} ` `} ` ` `  `// This code is contributed by vt_m `

## Python3

 `# python program to print all distinct ` `# elements in a given array ` ` `  `def` `printDistinct(arr, n): ` ` `  `    ``# Pick all elements one by one ` `    ``for` `i ``in` `range``(``0``, n): ` ` `  `        ``# Check if the picked element  ` `        ``# is already printed ` `        ``d ``=` `0` `        ``for` `j ``in` `range``(``0``, i): ` `            ``if` `(arr[i] ``=``=` `arr[j]): ` `                ``d ``=` `1` `                ``break` ` `  `        ``# If not printed earlier, ` `        ``# then print it ` `        ``if` `(d ``=``=` `0``): ` `            ``print``(arr[i]) ` `     `  `# Driver program to test above function ` `arr ``=` `[``6``, ``10``, ``5``, ``4``, ``9``, ``120``, ``4``, ``6``, ``10``] ` `n ``=` `len``(arr) ` `printDistinct(arr, n) ` ` `  `# This code is contributed by Sam007. `

## C#

 `// C# program to print all distinct ` `// elements in a given array ` `using` `System; ` ` `  `class` `GFG { ` ` `  `    ``static` `void` `printDistinct(``int` `[]arr, ``int` `n) ` `    ``{ ` `         `  `        ``// Pick all elements one by one ` `        ``for` `(``int` `i = 0; i < n; i++) ` `        ``{ ` `             `  `            ``// Check if the picked element  ` `            ``// is already printed ` `            ``int` `j; ` `            ``for` `(j = 0; j < i; j++) ` `                ``if` `(arr[i] == arr[j]) ` `                     ``break``; ` `     `  `            ``// If not printed earlier,  ` `            ``// then print it ` `            ``if` `(i == j) ` `            ``Console.Write(arr[i] + ``" "``); ` `        ``} ` `    ``} ` `     `  `    ``// Driver program ` `    ``public` `static` `void` `Main ()  ` `    ``{ ` `        ``int` `[]arr = {6, 10, 5, 4, 9, 120, ` `                                  ``4, 6, 10}; ` `        ``int` `n = arr.Length; ` `         `  `        ``printDistinct(arr, n); ` ` `  `    ``} ` `} ` ` `  `// This code is contributed by Sam007. `

## PHP

 ` `

Output:

`6 10 5 4 9 120`

Time Complexity of above solution is O(n2). We can Use Sorting to solve the problem in O(nLogn) time. The idea is simple, first sort the array so that all occurrences of every element become consecutive. Once the occurrences become consecutive, we can traverse the sorted array and print distinct elements in O(n) time. Following is the implementation of the idea.

## C++

 `// C++ program to print all distinct elements in a given array ` `#include ` `#include ` `using` `namespace` `std; ` ` `  `void` `printDistinct(``int` `arr[], ``int` `n) ` `{ ` `    ``// First sort the array so that all occurrences become consecutive ` `    ``sort(arr, arr + n); ` ` `  `    ``// Traverse the sorted array ` `    ``for` `(``int` `i=0; i

## Java

 `// Java program to print all distinct  ` `// elements in a given array ` `import` `java.io.*; ` `import` `java .util.*; ` ` `  `class` `GFG  ` `{ ` `    ``static` `void` `printDistinct(``int` `arr[], ``int` `n) ` `    ``{ ` `        ``// First sort the array so that  ` `        ``// all occurrences become consecutive ` `        ``Arrays.sort(arr); ` `     `  `        ``// Traverse the sorted array ` `        ``for` `(``int` `i = ``0``; i < n; i++) ` `        ``{ ` `            ``// Move the index ahead while  ` `            ``// there are duplicates ` `            ``while` `(i < n - ``1` `&& arr[i] == arr[i + ``1``]) ` `                ``i++; ` `     `  `            ``// print last occurrence of  ` `            ``// the current element ` `            ``System.out.print(arr[i] +``" "``); ` `        ``} ` `    ``} ` `     `  `    ``// Driver program  ` `    ``public` `static` `void` `main (String[] args)  ` `    ``{ ` `        ``int` `arr[] = {``6``, ``10``, ``5``, ``4``, ``9``, ``120``, ``4``, ``6``, ``10``}; ` `        ``int` `n = arr.length; ` `        ``printDistinct(arr, n); ` ` `  `    ``} ` `} ` ` `  `// This code is contributed by vt_m `

## C#

 `// C# program to print all distinct  ` `// elements in a given array ` `using` `System; ` ` `  `class` `GFG { ` ` `  `    ``static` `void` `printDistinct(``int` `[]arr, ``int` `n) ` `    ``{ ` `         `  `        ``// First sort the array so that  ` `        ``// all occurrences become consecutive ` `        ``Array.Sort(arr); ` `     `  `        ``// Traverse the sorted array ` `        ``for` `(``int` `i = 0; i < n; i++) ` `        ``{ ` `             `  `            ``// Move the index ahead while  ` `            ``// there are duplicates ` `            ``while` `(i < n - 1 && arr[i] == arr[i + 1]) ` `                ``i++; ` `     `  `            ``// print last occurrence of  ` `            ``// the current element ` `            ``Console.Write(arr[i] + ``" "``); ` `        ``} ` `    ``} ` `     `  `    ``// Driver program  ` `    ``public` `static` `void` `Main ()  ` `    ``{ ` `        ``int` `[]arr = {6, 10, 5, 4, 9, 120, 4, 6, 10}; ` `        ``int` `n = arr.Length; ` `         `  `        ``printDistinct(arr, n); ` `    ``} ` `} ` ` `  `// This code is contributed by Sam007. `

## PHP

 ` `

Output:

`4 5 6 9 10 120`

We can Use Hashing to solve this in O(n) time on average. The idea is to traverse the given array from left to right and keep track of visited elements in a hash table. Following is the implementation of the idea.

## C++

 `/* CPP program to print all distinct elements  ` `   ``of a given array */` `#include ` `using` `namespace` `std; ` ` `  `// This function prints all distinct elements ` `void` `printDistinct(``int` `arr[],``int` `n) ` `{ ` `    ``// Creates an empty hashset ` `    ``unordered_set<``int``> s; ` ` `  `    ``// Traverse the input array ` `    ``for` `(``int` `i=0; i

## Java

 `/* Java program to print all distinct elements of a given array */` `import` `java.util.*; ` ` `  `class` `Main ` `{ ` `    ``// This function prints all distinct elements ` `    ``static` `void` `printDistinct(``int` `arr[]) ` `    ``{ ` `        ``// Creates an empty hashset ` `        ``HashSet set = ``new` `HashSet<>(); ` ` `  `        ``// Traverse the input array ` `        ``for` `(``int` `i=``0``; i

## C#

 `// C# program to print all distinct ` `// elements of a given array  ` `using` `System; ` `using` `System.Collections.Generic; ` ` `  `class` `GFG ` `{ ` `// This function prints all  ` `// distinct elements  ` `public` `static` `void` `printDistinct(``int``[] arr) ` `{ ` `    ``// Creates an empty hashset  ` `    ``HashSet<``int``> ``set` `= ``new` `HashSet<``int``>(); ` ` `  `    ``// Traverse the input array  ` `    ``for` `(``int` `i = 0; i < arr.Length; i++) ` `    ``{ ` `        ``// If not present, then put it  ` `        ``// in hashtable and print it  ` `        ``if` `(!``set``.Contains(arr[i])) ` `        ``{ ` `            ``set``.Add(arr[i]); ` `            ``Console.Write(arr[i] + ``" "``); ` `        ``} ` `    ``} ` `} ` ` `  `// Driver Code ` `public` `static` `void` `Main(``string``[] args) ` `{ ` `    ``int``[] arr = ``new` `int``[] {10, 5, 3, 4, 3, 5, 6}; ` `    ``printDistinct(arr); ` `} ` `} ` ` `  `// This code is contributed by Shrikant13 `

Output:

`10 5 3 4 6 `

One more advantage of hashing over sorting is, the elements are printed in same order as they are in input array.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

This article is attributed to GeeksforGeeks.org

## tags:

Arrays Hash Sorting Arrays Hash Sorting

code

load comments