![]() |
VOOZH | about |
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
The simple Solution is to sort the array first, then traverse the array and print only first occurrences of elements.
Algorithm:
Below is the implementation of the approach:
1 2 3
Time Complexity: O(n * logn) as sort function has been called which takes O(n * logn) time. Here, n is size of the input array.
Auxiliary Space: O(1) as no extra space has been used.
Another Approach is to use set in C++ STL.
Implementation:
1 2 3
Complexity Analysis: