
At Stripe, an existing utility scans a list of integers with nested loops to find values that appear more than once. Rewrite it for better performance.
Implement a function find_duplicates(nums) that returns all distinct duplicate values in ascending order, along with how many times each value appears.
nums, a list of integers[value, count] for every integer that appears at least twicevalue in ascending orderExample 1
Input: nums = [4, 1, 2, 1, 2, 2, 5]
Output: [[1, 2], [2, 3]]
Explanation: 1 appears 2 times and 2 appears 3 times. Other values appear once.
Example 2
Input: nums = [7, 7, 7, 7]
Output: [[7, 4]]
Explanation: Only 7 is duplicated, and it appears 4 times.
0 <= len(nums) <= 10^5-10^9 <= nums[i] <= 10^9nums = [4, 1, 2, 1, 2, 2, 5]Output[[1, 2], [2, 3]]WhyThe value 1 appears twice and 2 appears three times; all other values appear once.nums = [7, 7, 7, 7]Output[[7, 4]]WhyOnly one distinct value exists, and it appears four times.nums = [3, 1, 4, 5]Output[]WhyNo value appears more than once, so the result is empty.0 <= len(nums) <= 10^5-10^9 <= nums[i] <= 10^9Return each duplicated value once with its total frequencyOutput must be sorted by value in ascending orderdef find_duplicates(nums):