Posted on by Kalkicode
Code Hash

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
Count occurrences of array elements

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

  1. Define a function display that takes an array arr as a parameter and prints all the elements in the array.
  2. Define a function frequency that takes an array arr as a parameter and finds the frequency of each element in the array using a Map.
  3. Call the display function with the array arr as a parameter to print the given array.
  4. Create an empty Map called map.
  5. For each element num in the array arr, do the following:
    • If map contains the key num, increment its value (frequency) by 1.
    • If map does not contain the key num, add num as a new key with a value of 1.
  6. Print "Occurrence".
  7. For each key key in the map, do the following:
    • Print the key and its value (frequency) using map.get(key).

Code Solution

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.

Comment

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