Print sorted distinct elements of array
Last Updated :
11 Aug, 2023
Improve
Given an array that might contain duplicates, print all distinct elements in sorted order.
Examples:
Input : 1, 3, 2, 2, 1
Output : 1 2 3
Input : 1, 1, 1, 2, 2, 3
Output : 1 2 3
The simple Solution is to sort the array first, then traverse the array and print only first occurrences of elements.
Algorithm:
- Sort the given array in non-descending order.
- Traverse the sorted array from left to right, and for each element do the following:
a. If the current element is not equal to the previous element, print the current element.
b. Otherwise, skip the current element and move on to the next one. - Stop when the end of the array is reached.
Below is the implementation of the approach:
C++
// CPP program to print sorted distinct // elements. #include <bits/stdc++.h> using namespace std; // Function to print sorted distinct // elements void printDistinct( int arr[], int n) { // Sort the array sort(arr, arr + n); // Traverse the sorted array for ( int i = 0; i < n; i++) { // If the current element is not equal to the previous // element, print it if (i == 0 || arr[i] != arr[i - 1]) cout << arr[i] << " " ; } } // Driver's code int main() { // Input array int arr[] = { 1, 3, 2, 2, 1 }; int n = sizeof (arr) / sizeof (arr[0]); // Function call printDistinct(arr, n); return 0; } |
Java
// Java program to print sorted distinct // elements. import java.util.*; class GFG { // Function to print sorted distinct // elements static void printDistinct( int arr[], int n) { // Sort the array Arrays.sort(arr); // Traverse the sorted array for ( int i = 0 ; i < n; i++) { // If the current element is not equal to the // previous element, print it if (i == 0 || arr[i] != arr[i - 1 ]) System.out.print(arr[i] + " " ); } } // Driver's code public static void main(String[] args) { // Input array int arr[] = { 1 , 3 , 2 , 2 , 1 }; int n = arr.length; // Function call printDistinct(arr, n); } } |
Python3
# Python program to print sorted distinct elements def printDistinct(arr): # Sort the array arr.sort() # Traverse the sorted array for i in range ( len (arr)): # If the current element is not equal to the previous # element, print it if i = = 0 or arr[i] ! = arr[i - 1 ]: print (arr[i]) # Driver code arr = [ 1 , 3 , 2 , 2 , 1 ] printDistinct(arr) |
C#
using System; using System.Linq; class GFG { // Function to print sorted distinct elements static void printDistinct( int [] arr, int n) { // Sort the array Array.Sort(arr); // Traverse the sorted array for ( int i = 0; i < n; i++) { // If the current element is not equal to the previous element, print it if (i == 0 || arr[i] != arr[i - 1]) Console.Write(arr[i] + " " ); } } // Driver's code public static void Main() { // Input array int [] arr = { 1, 3, 2, 2, 1 }; int n = arr.Length; // Function call printDistinct(arr, n); } } // THIS CODE IS CONTRIBUTED BY CHANDAN AGARWAL |
Javascript
// Javascript program to print sorted distinct // elements. function printDistinct(arr) { // Sort the array arr.sort((a, b) => a - b); // Traverse the sorted array for (let i = 0; i < arr.length; i++) { // If the current element is not equal to the previous // element, print it if (i === 0 || arr[i] !== arr[i - 1]) { console.log(arr[i]); } } } // Driver code const arr = [1, 3, 2, 2, 1]; printDistinct(arr); |
Output
1 2 3
Time Complexity: O(n * logn) as sort function has been called which takes O(n * logn) time. Here, n is size of the input array.
Auxiliary Space: O(1) as no extra space has been used.
Another Approach is to use set in C++ STL.
Implementation:
C++
// CPP program to print sorted distinct // elements. #include <bits/stdc++.h> using namespace std; void printRepeating( int arr[], int size) { // Create a set using array elements set< int > s(arr, arr + size); // Print contents of the set. for ( auto x : s) cout << x << " " ; } // Driver code int main() { int arr[] = { 1, 3, 2, 2, 1 }; int n = sizeof (arr) / sizeof (arr[0]); printRepeating(arr, n); return 0; } |
Java
// Java program to print sorted distinct // elements. import java.io.*; import java.util.*; public class GFG { static void printRepeating(Integer []arr, int size) { // Create a set using array elements SortedSet<Integer> s = new TreeSet<>(); Collections.addAll(s, arr); // Print contents of the set. System.out.print(s); } // Driver code public static void main(String args[]) { Integer []arr = { 1 , 3 , 2 , 2 , 1 }; int n = arr.length; printRepeating(arr, n); } } // This code is contributed by // Manish Shaw (manishshaw1) |
Python3
# Python3 program to print # sorted distinct elements. def printRepeating(arr,size): # Create a set using array elements s = set () for i in range (size): if arr[i] not in s: s.add(arr[i]) # Print contents of the set. for i in s: print (i,end = " " ) # Driver code if __name__ = = '__main__' : arr = [ 1 , 3 , 2 , 2 , 1 ] size = len (arr) printRepeating(arr,size) # This code is contributed by # Shrikant13 |
C#
// C# program to print sorted distinct // elements. using System; using System.Collections.Generic; using System.Linq; class GFG { static void printRepeating( int []arr, int size) { // Create a set using array elements SortedSet< int > s = new SortedSet< int >(arr); // Print contents of the set. foreach ( var n in s) { Console.Write(n + " " ); } } // Driver code public static void Main() { int []arr = {1, 3, 2, 2, 1}; int n = arr.Length; printRepeating(arr, n); } } // This code is contributed by // Manish Shaw (manishshaw1) |
Javascript
<script> // Javascript program to print sorted distinct // elements. function printRepeating(arr, size) { // Create a set using array elements var s = new Set(arr); // Print contents of the set. [...s].sort((a, b) => a - b).forEach(x => { document.write(x + " " ) }); } // Driver code var arr = [ 1, 3, 2, 2, 1 ]; var n = arr.length; printRepeating(arr, n); // This code is contributed by itsok </script> |
Output
1 2 3
Complexity Analysis:
- Time Complexity: O(nlogn).
- Auxiliary Space: O(n)