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.
- Print All Distinct Elements of a given integer array
- Absolute distinct count in a sorted array
- Sort a nearly sorted (or K sorted) array
- 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
- Print array of strings in sorted order without copying one string into another
- Print k different sorted permutations of a given array
- Find sum of non-repeating (distinct) elements in an array
- Product of non-repeating (distinct) elements in an Array
- Count distinct elements in an array
- Find minimum changes required in an array for it to contain k distinct elements
- Maximize count of distinct elements possible in an Array from the given operation
- Check if K distinct array elements form an odd sum
- Sort an almost sorted array where only two elements are swapped
- Check if given array is almost sorted (elements are at-most one position away)
- Remove elements to make array sorted
- Find the number of elements greater than k in a sorted array
- Minimum elements to be removed from the ends to make the array sorted
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.