Given an unsorted array of integers which may contain repeated elements, sort the elements in descending order of some of its occurrence. If there exists more than one element whose sum of occurrences are the same then, the one which is greater will come first.
Input: arr = [2, 4, 1, 2, 4, 2, 10]
Output: arr = [10, 4, 4, 2, 2, 2, 1]
Here, 2 appears 3 times, 4 appears 2 times, 1 appears 1 time and 10 appears 1 time.
Sum of all occurrences of 2 in given array = 2 * 3 = 6,
Sum of all occurrences of 4 = 4 * 2 = 8,
Sum of all occurrences of 10 = 10 * 1 = 10,
Sum of all occurrences of 1 = 1 * 1 = 1.
Therefore sorting the array in descending order based on the above sum = [ 10, 4, 4, 2, 2, 2, 1 ]
Input2: arr = [2, 3, 2, 3, 2, 1, 1, 9, 6, 9, 1, 2]
Output2: [9, 9, 2, 2, 2, 2, 6, 3, 3, 1, 1, 1]
- Create a map that will map elements to it appearence ie if a occur one time then a will map to a but if a occur m times then a will be map to a*m.
- After doing the mapping sort the dictionary in descending order based on their values not keys. In case of tie, sort based on values.
- Finaly copy the sorted dictionary keys in final output array and there frequency is obtain based on their key-value pairs i.e. if a is mapped to a*m it means we need to include a, m times in output array.
Below is the implementation of the above approach:
[3, 3, 3, 5, 2, 2, 1, 1]
Time Complexity: O(N*log N).
Space Complexity: O(N).
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.
- Sort all even numbers in ascending order and then sort all odd numbers in descending order
- Sort first k values in ascending order and remaining n-k values in descending order
- Sort prime numbers of an array in descending order
- Sorting rows of matrix in ascending order followed by columns in descending order
- Sorting rows of matrix in descending order followed by columns in ascending order
- Sorting Vector of Pairs in C++ | Set 2 (Sort in descending order by first and second)
- Sort first half in ascending and second half in descending order | 1
- Program to sort string in descending order
- Stable sort for descending order
- Sort first half in ascending and second half in descending order | Set 2
- Sort Matrix in alternating ascending and descending order rowwise
- Rearrange array such that difference of adjacent elements is in descending order
- Sort an array of strings based on the given order
- Sorting 2D Vector in C++ | Set 2 (In descending order by row and column)
- Rank of all elements in a Stream in descending order when they arrive
- Sort a linked list that is sorted alternating ascending and descending orders?
- Print the last occurrence of elements in array in relative order
- Print characters having odd frequencies in order of occurrence
- Concatenate the strings in an order which maximises the occurrence of subsequence "ab"
- Print characters having even frequencies in order of occurrence
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.
Improved By : Akanksha_Rai