Video Discription |
Given an array of integers. Write a code to sort an array elements in descending order of their frequency.
Paypal - https://www.paypal.me/programmingtutorials
Website - https://webrewrite.com/
If any two or more elements have same frequency then print the element which comes first.
For example -
Example 1:
Input : [5, 3, 4, 5, 8, 1, 1, 5]
Output : [5, 5, 5, 1, 1, 3, 4, 8]
The frequency of elements are [5 : 3, 1 : 2, 3 : 1, 4 : 1, 8 : 1].
When we sort this array by frequency 5 comes first then 1. The element 3, 4 and 8 has same frequency. So, we put these elements based on which element occur first in an array.
Example 2:
Input : [8, 2, 3, 4, 8, 2, 8, 2, 4, 5, 2, 9, 3]
Output : [2, 2, 2, 2, 8, 8, 8, 3, 3, 4, 4, 5, 9]
The frequency of elements are [ 2 : 4, 8 : 3, 3 : 2, 4 : 2, 5 : 1, 9 : 1].
When we sort this array the order is 2, 8, 3, 4, 5, 9. _xM4gf3aPQk |