ExamAdda Logo

Frequency of Elements in an Array

Easy

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

1 <= arr.length <= 10^5
-10^9 <= arr[i] <= 10^9

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

Chatla Jithin Sri Surya

Written by: Chatla Jithin Sri Surya

<p>IIEST Shibpur | SDE | AI, ML &amp; Gen AI | DSA | Building Intelligent, Scalable Software</p>

Chatla Jithin Sri Surya

Technically reviewed by: Chatla Jithin Sri Surya

<p>IIEST Shibpur | SDE | AI, ML &amp; Gen AI | DSA | Building Intelligent, Scalable Software</p>

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.

Report a correction:editorial@examadda.org
Auto
Loading editor...
Input

Expected Output