Print sorted distinct elements of array - GeeksforGeeks

09 Jun, 2021
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
Simple Solution is to sort the array first, then traverse the array and print only first occurrences of elements.
Another Approach is to use set in C++ STL.  
C++
Java
Python3
C#
Javascript
// 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 codeint main(){    int arr[] = { 1, 3, 2, 2, 1 };    int n = sizeof(arr) / sizeof(arr[0]);    printRepeating(arr, n);    return 0;}
// 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 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 codeif __name__=='__main__':    arr = [1,3,2,2,1]    size = len(arr)    printRepeating(arr,size) # This code is contributed by# Shrikant13
// 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)
<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 codevar arr = [ 1, 3, 2, 2, 1 ];var n = arr.length; printRepeating(arr, n); // This code is contributed by itsok </script>
1 2 3
 
manishshaw1
shrikanth13
itsok
cpp-set
Arrays
Sorting
Arrays
Sorting
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Maximum and minimum of an array using minimum number of comparisons
Top 50 Array Coding Problems for Interviews
Stack Data Structure (Introduction and Program)
Introduction to Arrays
Multidimensional Arrays in Java