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
- Absolute distinct count in a sorted array
- Print uncommon elements from two sorted arrays
- 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 k different sorted permutations of a given array
- Count distinct elements after adding each element of First Array with Second Array
- Print all triplets in sorted array that form AP
- Check if an Array can be Sorted by picking only the corner Array elements
- Count distinct elements in an array
- Check if all array elements are distinct
- Distinct adjacent elements in an array
- Print array of strings in sorted order without copying one string into another
- Find sum of non-repeating (distinct) elements in an array
- Check if K distinct array elements form an odd sum
- Product of non-repeating (distinct) elements in an Array
- Third largest element in an array of distinct elements
- Distinct adjacent elements in a binary array
- Maximum absolute difference between distinct elements in an Array