Count the occurrence of array elements
The problem is to count the frequency of each element in an array. Given an array, we need to find how many times each unique element appears in the array.
Problem Statement
Given an array of integers, count the occurrence of each element and display the result.
Example
Consider the following example:
Input:
arr = [1, 3, 2, 1, 4, 2, 7, 9, 1, 3, 3, 4, 7]
Output:
1 3 2 1 4 2 7 9 1 3 3 4 7
Occurrence
1 : 3
2 : 2
3 : 3
4 : 2
7 : 2
9 : 1

Explanation:
- The given array is
[1, 3, 2, 1, 4, 2, 7, 9, 1, 3, 3, 4, 7]
. - The frequency of element 1 is 3 because it appears three times in the array.
- The frequency of element 2 is 2 because it appears two times in the array.
- The frequency of element 3 is 3 because it appears three times in the array.
- The frequency of element 4 is 2 because it appears two times in the array.
- The frequency of element 7 is 2 because it appears two times in the array.
- The frequency of element 9 is 1 because it appears one time in the array.
Idea to Solve the Problem
To count the occurrence of each element in the array, we can use a Map. We iterate through the array, and for each element, we check if it already exists as a key in the Map. If it does, we update its value (frequency) by incrementing it. If it doesn't exist, we add it as a new key with a frequency of 1. After processing all elements, the Map will contain the frequency of each unique element in the array.
Pseudocode
Function display(arr)
For each element in arr
Print the element
Function frequency(arr)
Call the display function with arr as a parameter
Create an empty HashMap called map
For each element in arr
If map contains the element as a key
Increment its value (frequency) by 1
Else
Add the element as a new key with a value of 1
Print "Occurrence"
For each key in map
Print the key and its value (frequency)
Main function
Initialize arr with the given array [1, 3, 2, 1, 4, 2, 7, 9, 1, 3, 3, 4, 7]
Call the frequency function with arr as a parameter
Algorithm
- Define a function
display
that takes an arrayarr
as a parameter and prints all the elements in the array. - Define a function
frequency
that takes an arrayarr
as a parameter and finds the frequency of each element in the array using a Map. - Call the
display
function with the arrayarr
as a parameter to print the given array. - Create an empty Map called
map
. - For each element
num
in the arrayarr
, do the following:- If
map
contains the keynum
, increment its value (frequency) by 1. - If
map
does not contain the keynum
, addnum
as a new key with a value of 1.
- If
- Print "Occurrence".
- For each key
key
in themap
, do the following:- Print the key and its value (frequency) using
map.get(key)
.
- Print the key and its value (frequency) using
Code Solution
-
1) Find the frequency of each element in an array in c++
2) Find the frequency of each element in an array in c#
3) Find the frequency of each element in an array in php
4) Find the frequency of each element in an array in java
5) Find the frequency of each element in an array in node js
6) Find the frequency of each element in an array in typescript
7) Find the frequency of each element in an list in python
8) Find the frequency of each element in an array in ruby
9) Find the frequency of each element in an array in scala
10) Find the frequency of each element in an array in swift
11) Find the frequency of each element in an array in kotlin
12) Find the frequency of each element in an array in golang
13) Find the frequency of each element in an array in vb.net
Time Complexity
The time complexity of the code is O(n), where n is the number of elements in the array. This is because we need to iterate through the array once to find the frequency of each element and store it in the Map. The operations in the Map (insertion, deletion, and retrieval) take constant time on average, making the overall time complexity linear in the number of elements.
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment