Asked inJPMorgan Chase & Co.,R&D Intern

Sort Elements by Frequency

Your task is to sort a list of repeated integers by their frequency in decreasing order. The element with the highest frequency should appear first. If two elements have the same frequency, maintain their original order from the list.

Example:

Input:
arr[] = {2, 5, 2, 8, 5, 6, 8, 8}
Output:
arr[] = {8, 8, 8, 2, 2, 5, 5, 6}
Explanation:

The integer '8' has the highest frequency and appears first. Both '2' and '5' have the same frequency, but '2' comes before '5' in the original array, hence it comes first in the sorted array.

Input:

The first line of input contains a single integer 'T', the number of test cases.
The first line of each test case contains a single integer 'N', the number of integers.
The next line contains 'N' space-separated integers, the elements of the list.

Output:

For each test case, print the list of elements sorted by decreasing frequency of repetition.

Constraints:

  • 1 ≤ T ≤ 10
  • 1 ≤ N ≤ 3000
  • 1 ≤ NUMS[i] ≤ 10^6
  • Time Limit: 1 sec

Note:

You do not need to print anything; it has already been taken care of. Just implement the given function.

Be the first one to answer
Add answer anonymously...
JPMorgan Chase & Co. R&D Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter