Given an array that might contain duplicates, print all distinct elements in sorted order.
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.
1 2 3
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Making elements distinct in a sorted array by minimum increments
- Print All Distinct Elements of a given integer array
- Generate all possible sorted arrays from alternate elements of two given sorted arrays
- Absolute distinct count in a sorted array
- Sort a nearly sorted (or K sorted) array
- Length of array pair formed where one contains all distinct elements and other all same elements
- Split an array containing N elements into K sets of distinct elements
- Print uncommon elements from two sorted arrays
- Form an array of distinct elements with each element as sum of an element from each array
- Count distinct elements after adding each element of First Array with Second Array
- Count all distinct pairs of repeating elements from the array for every array element
- Check if an Array can be Sorted by picking only the corner Array elements
- Print all distinct integers that can be formed by K numbers from a given array of N numbers
- Maximum number of partitions that can be sorted individually to make sorted
- Merge K sorted Doubly Linked List in Sorted Order
- Sum of distinct elements when elements are in range 1 to n
- Maximum distinct elements after removing k elements
- Print array of strings in sorted order without copying one string into another
- Print all triplets in sorted array that form AP
- Print k different sorted permutations of a given array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.