Frequency of Elements in an Array
Given an array of integers arr, find the frequency of every distinct element in the array.
Return the elements along with their frequencies as pairs [element, frequency].
The result must be sorted in ascending order of the elements.
Example 1
Input
arr = [1, 2, 2, 3, 1, 1]
Output
[[1, 3], [2, 2], [3, 1]]
Explanation
- Element 1 appears 3 times.
- Element 2 appears 2 times.
- Element 3 appears 1 time.
Example 2
Input
arr = [4, 4, 5, 5, 5, 6]
Output
[[4, 2], [5, 3], [6, 1]]
Explanation
- Element 4 appears 2 times.
- Element 5 appears 3 times.
- Element 6 appears 1 time
Constraints
Hints:
Hint 1
Use a HashMap to store each distinct element and its frequency.
Hint 2
Sort the distinct elements in ascending order before creating the final list of [element, frequency] pairs
Author & Technical Reviewer
Published
Nov 7, 2026
Last updated
Nov 7, 2026
Last technically reviewed
Nov 7, 2026
Content Verification Methodology
Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.
Expected Output