Frequency of each element of an array of small ranged values
Last Updated :
09 Sep, 2022
Given an array where elements in small range. The maximum element in the array does not go beyond size of array. Find frequencies of elements.
Examples:
Input : arr[] = {3, 1, 2, 3, 4, 5, 4}
Output: 1-->1
2-->1
3-->2
4-->2
5-->1
Input : arr[] = {1, 2, 2, 1, 2}
Output: 1-->2
2-->3
Input : arr[] = {1, 2, 4}
Output: 1-->1
2-->1
4-->1
A simple solution is to use two nested loops. For each element (from 1 to n where n is size of array), count how many times it appears. Time complexity of this solution is O(n*n)
A better solution is to use sorting. First sort the array, after sorting, linearly traverse the array and count occurrences of each element. Time complexity of this solution is O(n Log n)
An efficient solution is to use hashing. We insert every element in a hash table and increment frequency. Time complexity of this solution is O(n). Please see Frequency Measuring Techniques for Competitive Programming for implementation.
An efficient solution for limited range
The hashing based solution is fast, but requires hash function computations, etc. If we know that range is small, we use direct address table where we create an array of size equal to maximum value and use array elements as index.
Following is the implementation for above explanation:
C++
#include <bits/stdc++.h>
using namespace std;
void frequencyOfEach( int * arr, int n)
{
int max = *max_element(arr, arr + n);
int hash[max + 1] = { 0 };
for ( int i = 0; i < n; i++) {
hash[arr[i]]++;
}
for ( int i = 0; i <= max; i++) {
if (hash[i] != 0)
cout << i << "-->" << hash[i] << "\n" ;
}
}
int main()
{
int arr[] = { 5, 2, 2, 3, 5, 1, 1, 5, 3, 4 };
int n = sizeof (arr) / sizeof (arr[0]);
frequencyOfEach(arr, n);
return 0;
}
|
Java
import java.util.*;
class solution
{
static void frequencyOfEach( int []arr, int n)
{
int max = Integer.MIN_VALUE;
for ( int i = 0 ;i<n;i++)
{
if (arr[i]>max)
max = arr[i];
}
int []hash = new int [max + 1 ];
Arrays.fill(hash, 0 );
for ( int i = 0 ; i < n; i++) {
hash[arr[i]]++;
}
for ( int i = 0 ; i <= max; i++) {
if (hash[i] != 0 )
System.out.println(i+ "-->" +hash[i]);
}
}
public static void main(String args[])
{
int []arr = { 5 , 2 , 2 , 3 , 5 , 1 , 1 , 5 , 3 , 4 };
int n = arr.length;
frequencyOfEach(arr, n);
}
}
|
Python3
def frequencyOfEach(arr, n) :
max_element = max (arr)
hash = [ 0 ] * (max_element + 1 )
for i in range (n) :
hash [arr[i]] + = 1
for i in range (max_element + 1 ) :
if ( hash [i] ! = 0 ) :
print (i, "-->" , hash [i])
if __name__ = = "__main__" :
arr = [ 5 , 2 , 2 , 3 , 5 ,
1 , 1 , 5 , 3 , 4 ]
n = len (arr)
frequencyOfEach(arr, n);
|
C#
using System;
public class solution{
static void frequencyOfEach( int []arr, int n)
{
int max = int .MinValue;
for ( int i = 0;i<n;i++)
{
if (arr[i]>max)
max = arr[i];
}
int []hash = new int [max + 1];
for ( int i = 0; i < n; i++) {
hash[arr[i]]++;
}
for ( int i = 0; i <= max; i++) {
if (hash[i] != 0)
Console.WriteLine (i+ "-->" +hash[i]);
}
}
public static void Main()
{
int []arr = { 5, 2, 2, 3, 5, 1, 1, 5, 3, 4 };
int n = arr.Length;
frequencyOfEach(arr, n);
}
}
|
PHP
<?php
function frequencyOfEach(& $arr , $n )
{
$max = max( $arr );
$hash = array_fill (0, $max + 1, NULL);
for ( $i = 0; $i < $n ; $i ++)
{
$hash [ $arr [ $i ]]++;
}
for ( $i = 0; $i <= $max ; $i ++)
{
if ( $hash [ $i ] != 0)
echo $i . "-->" . $hash [ $i ] . "\n" ;
}
}
$arr = array (5, 2, 2, 3, 5, 1, 1, 5, 3, 4 );
$n = sizeof( $arr );
frequencyOfEach( $arr , $n );
?>
|
Javascript
<script>
function frequencyOfEach(arr,n)
{
let max = Number.MIN_VALUE;
for (let i = 0; i < n; i++)
{
if (arr[i] > max)
max = arr[i];
}
let hash = new Array(max + 1);
for (let i = 0; i < hash.length; i++)
{
hash[i] = 0;
}
for (let i = 0; i < n; i++)
{
hash[arr[i]]++;
}
for (let i = 0; i <= max; i++) {
if (hash[i] != 0)
document.write(i+ "-->" +hash[i]+ "<br>" );
}
}
let arr=[5, 2, 2, 3, 5, 1, 1, 5, 3, 4];
let n = arr.length;
frequencyOfEach(arr, n);
</script>
|
Output
1-->2
2-->2
3-->2
4-->1
5-->3
Complexity Analysis:
- Time Complexity: O(max_value)
- Auxiliary Space: O(max_value)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...