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
Select
Add answer anonymously...
JPMorgan Chase & Co. R&D Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits