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
- Making elements distinct in a sorted array by minimum increments
- Print All Distinct Elements of a given integer array
- Absolute distinct count in a sorted array
- Print uncommon elements from two sorted arrays
- Count distinct elements in an array
- Distinct adjacent elements in an array
- Check if all array elements are distinct
- Product of non-repeating (distinct) elements in an Array
- Distinct adjacent elements in a binary array
- Third largest element in an array of distinct elements
- Find sum of non-repeating (distinct) elements in an array
- Print k different sorted permutations of a given array
- Find minimum changes required in an array for it to contain k distinct elements
- Print all triplets in sorted array that form AP
- Count subarrays having total distinct elements same as original 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 email@example.com. 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.