Print sorted distinct elements of array
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++
#include <bits/stdc++.h>
using namespace std;
void printDistinct( int arr[], int n) {
sort(arr, arr + n);
for ( int i = 0; i < n; i++) {
if (i == 0 || arr[i] != arr[i - 1])
cout << arr[i] << " " ;
}
}
int main() {
int arr[] = { 1, 3, 2, 2, 1 };
int n = sizeof (arr) / sizeof (arr[0]);
printDistinct(arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG {
static void printDistinct( int arr[], int n) {
Arrays.sort(arr);
for ( int i = 0 ; i < n; i++) {
if (i == 0 || arr[i] != arr[i - 1 ])
System.out.print(arr[i] + " " );
}
}
public static void main(String[] args) {
int arr[] = { 1 , 3 , 2 , 2 , 1 };
int n = arr.length;
printDistinct(arr, n);
}
}
|
Python3
def printDistinct(arr):
arr.sort()
for i in range ( len (arr)):
if i = = 0 or arr[i] ! = arr[i - 1 ]:
print (arr[i])
arr = [ 1 , 3 , 2 , 2 , 1 ]
printDistinct(arr)
|
C#
using System;
using System.Linq;
class GFG {
static void printDistinct( int [] arr, int n) {
Array.Sort(arr);
for ( int i = 0; i < n; i++) {
if (i == 0 || arr[i] != arr[i - 1])
Console.Write(arr[i] + " " );
}
}
public static void Main() {
int [] arr = { 1, 3, 2, 2, 1 };
int n = arr.Length;
printDistinct(arr, n);
}
}
|
Javascript
function printDistinct(arr) {
arr.sort((a, b) => a - b);
for (let i = 0; i < arr.length; i++) {
if (i === 0 || arr[i] !== arr[i - 1]) {
console.log(arr[i]);
}
}
}
const arr = [1, 3, 2, 2, 1];
printDistinct(arr);
|
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++
#include <bits/stdc++.h>
using namespace std;
void printRepeating( int arr[], int size)
{
set< int > s(arr, arr + size);
for ( auto x : s)
cout << x << " " ;
}
int main()
{
int arr[] = { 1, 3, 2, 2, 1 };
int n = sizeof (arr) / sizeof (arr[0]);
printRepeating(arr, n);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
public class GFG {
static void printRepeating(Integer []arr, int size)
{
SortedSet<Integer> s = new TreeSet<>();
Collections.addAll(s, arr);
System.out.print(s);
}
public static void main(String args[])
{
Integer []arr = { 1 , 3 , 2 , 2 , 1 };
int n = arr.length;
printRepeating(arr, n);
}
}
|
Python3
def printRepeating(arr,size):
s = set ()
for i in range (size):
if arr[i] not in s:
s.add(arr[i])
for i in s:
print (i,end = " " )
if __name__ = = '__main__' :
arr = [ 1 , 3 , 2 , 2 , 1 ]
size = len (arr)
printRepeating(arr,size)
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
class GFG {
static void printRepeating( int []arr, int size)
{
SortedSet< int > s = new SortedSet< int >(arr);
foreach ( var n in s)
{
Console.Write(n + " " );
}
}
public static void Main()
{
int []arr = {1, 3, 2, 2, 1};
int n = arr.Length;
printRepeating(arr, n);
}
}
|
Javascript
<script>
function printRepeating(arr, size)
{
var s = new Set(arr);
[...s].sort((a, b) => a - b).forEach(x => {
document.write(x + " " )
});
}
var arr = [ 1, 3, 2, 2, 1 ];
var n = arr.length;
printRepeating(arr, n);
</script>
|
Complexity Analysis:
- Time Complexity: O(nlogn).
- Auxiliary Space: O(n)
Last Updated :
11 Aug, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...