DEV Community

Cover image for 1636. Sort Array by Increasing Frequency
MD ARIFUL HAQUE
MD ARIFUL HAQUE

Posted on

2

1636. Sort Array by Increasing Frequency

1636. Sort Array by Increasing Frequency

Easy

Given an array of integers nums, sort the array in increasing order based on the frequency of the values. If multiple values have the same frequency, sort them in decreasing order.

Return the sorted array.

Example 1:

  • Input: nums = [1,1,2,2,2,3]
  • Output: [3,1,1,2,2,2]
  • Explanation: '3' has a frequency of 1, '1' has a frequency of 2, and '2' has a frequency of 3.

Example 2:

  • Input: nums = [2,3,1,3,2]
  • Output: [1,3,3,2,2]
  • Explanation: '2' and '3' both have a frequency of 2, so they are sorted in decreasing order.

Example 3:

  • Input: nums = [-1,1,-6,4,5,-6,1,4,1]
  • Output: [5,-1,4,4,-6,-6,1,1,1]

Constraints:

  • 1 <= nums.length <= 100
  • -100 <= nums[i] <= 100

Hint:

  1. Count the frequency of each value.
  2. Use a custom comparator to compare values by their frequency. If two values have the same frequency, compare their values.

Solution:

To solve this problem, we can follow these steps:

  1. Count the frequency of each value in the input array.
  2. Use a custom comparator to sort the values based on their frequency first, and if the frequencies are the same, sort by value in decreasing order.

Let's implement this solution in PHP: 1636. Sort Array by Increasing Frequency

<?php
// Test cases
$nums1 = [1,1,2,2,2,3];
$nums2 = [2,3,1,3,2];
$nums3 = [-1,1,-6,4,5,-6,1,4,1];

print_r(frequencySort($nums1)); // Output: [3, 1, 1, 2, 2, 2]
print_r(frequencySort($nums2)); // Output: [1, 3, 3, 2, 2]
print_r(frequencySort($nums3)); // Output: [5, -1, 4, 4, -6, -6, 1, 1, 1]
?>
Enter fullscreen mode Exit fullscreen mode

Explanation:

  1. array_count_values($nums):
    • This function counts the frequency of each value in the array. It returns an associative array where the keys are the values from the input array and the values are their respective counts.
  2. usort($nums, function($a, $b) use ($frequency)):
    • This function sorts the array using a custom comparator.
    • if ($frequency[$a] == $frequency[$b]): If the frequency of a and b is the same, compare their values directly. Return ($b - $a) to sort in decreasing order.
    • return $frequency[$a] - $frequency[$b]: If the frequency is not the same, return the difference to sort by frequency in increasing order.

This approach ensures that the array is sorted according to the specified rules.

Contact Links

If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!

If you want more helpful content like this, feel free to follow me:

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →