Grokking Data Structures & Algorithms for Coding Interviews
Ask Author
Back to course home

0% completed

Sort Array by Increasing Frequency (easy)
Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Problem Statement

Given an array nums containing the integers, return the resultant array after sorting it in increasing order based on the frequency of the values. If two numbers have the same frequency, they should be sorted in descending numerical order.

Examples

Example 1:

  • Input: nums = [4, 4, 6, 2, 2, 2]
  • ExpectedOutput: [6, 4, 4, 2, 2, 2]
  • Justification: Here, '6' appears once, '4' appears twice, and '2' appears three times. Thus, numbers are first sorted by frequency and then by value when frequencies tie.

Example 2:

  • Input: nums =

.....

.....

.....

Like the course? Get enrolled and start learning!

Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible