Fill an array based on frequency where elements are in range from 0 to n-1
Last Updated :
13 Sep, 2023
Given an array of positive integers with duplicate allowed. The array contains elements from range 0 to n-1. The task is to fill the array such that arr[i] contains the frequency of i.
Examples :
Input : arr[] = {1, 4, 3, 4, 1, 1, 4, 4, 4, 7}
Output : arr[] = {0, 3, 0, 1, 5, 0, 0, 1, 0, 0}
Here 0 appears 0 times, so arr[0] is 0
1 appears 3 times
2 appears 0 times
3 appears 0 times
4 appears 5 times
..........
Input : arr[] = {1, 2, 3, 4, 1, 1, 4, 5, 6, 7}
Output : arr[] = {0, 3, 1, 1, 2, 1, 1, 1, 0, 0}
A simple solution of this problem is to run two loops. The outer loop picks elements one by one. The inner loop counts frequency of the picked element and stores frequency in final array.
An efficient solution is to use an auxiliary array of size n.
1) Create an array temp[0..n-1] and
initialize it as 0.
2) Traverse given array and do following
for every element arr[i].
temp[arr[i]]++
3) Copy temp[] to arr[].
Below is the implementation of above steps.
C++
// C++ program to fill an array with frequencies.
#include<bits/stdc++.h>
using namespace std;
// Fills arr[] with frequencies of elements
void fillWithFreq(int arr[], int n)
{
int temp[n];
memset(temp, 0, sizeof(temp));
// Fill temp with frequencies
for (int i=0; i<n; i++)
temp[arr[i]] += 1;
// Copy temp to array
for (int i=0; i<n; i++)
arr[i] = temp[i];
}
// Driver code
int main()
{
int arr[] = {5, 2, 3, 4, 5, 5, 4, 5, 6, 7};
int n = sizeof(arr)/sizeof(arr[0]);
fillWithFreq(arr, n);
for (int i=0; i<n; i++)
cout << arr[i] << " ";
return 0;
}
Java
// Java program to fill an array with frequencies.
import java.util.Arrays;
class GFG {
// Fills arr[] with frequencies of elements
static void fillWithFreq(int arr[], int n)
{
int temp[]=new int[n];
Arrays.fill(temp, 0);
// Fill temp with frequencies
for (int i = 0; i < n; i++)
temp[arr[i]] += 1;
// Copy temp to array
for (int i = 0; i < n; i++)
arr[i] = temp[i];
}
// Driver method
public static void main(String[] args)
{
int arr[] = {5, 2, 3, 4, 5, 5, 4, 5, 6, 7};
int n = arr.length;
fillWithFreq(arr, n);
for (int i=0; i<n; i++)
System.out.print(arr[i] + " ");
}
}
// This code is contributed by Anant Agarwal.
Python3
# Python3 program to fill an
# array with frequencies.
# Fills arr[] with frequencies of elements
def fillWithFreq(arr, n):
temp = [0 for i in range(n)]
# Fill temp with frequencies
for i in range(n):
temp[arr[i]] += 1
# Copy temp to array
for i in range(n):
arr[i] = temp[i]
# Driver Code
arr = [5, 2, 3, 4, 5, 5, 4, 5, 6, 7]
n = len(arr)
fillWithFreq(arr, n)
for i in range(n):
print(arr[i], end = " ")
# This code is contributed by Anant Agarwal.
C#
// C# program to fill an
// array with frequencies.
using System;
class GFG {
// Fills arr[] with frequencies of elements
static void fillWithFreq(int []arr, int n)
{
int []temp = new int[n];
for(int i = 0; i < n; i++)
temp[i] = 0;
// Fill temp with frequencies
for (int i = 0; i < n; i++)
temp[arr[i]] += 1;
// Copy temp to array
for (int i = 0; i < n; i++)
arr[i] = temp[i];
}
// Driver method
public static void Main()
{
int []arr = {5, 2, 3, 4, 5,
5, 4, 5, 6, 7};
int n = arr.Length;
// Function calling
fillWithFreq(arr, n);
for (int i = 0; i < n; i++)
Console.Write(arr[i] + " ");
}
}
// This code is contributed by nitin mittal.
PHP
<?php
// PHP program to fill an
// array with frequencies.
// Fills arr[] with frequencies
// of elements
function fillWithFreq($arr, $n)
{
$temp = array();
for($i = 0; $i < $n; $i++)
$temp[$i] = 0;
// Fill temp with frequencies
for ($i = 0; $i < $n; $i++)
$temp[$arr[$i]] += 1;
// Copy temp to array
for ($i = 0; $i < $n; $i++)
$arr[$i] = $temp[$i];
// print array
for ($i = 0; $i < $n; $i++)
echo $arr[$i] . " ";
}
// Driver method
$arr = array(5, 2, 3, 4, 5,
5, 4, 5, 6, 7);
$n = count($arr);
// Function calling
fillWithFreq($arr, $n);
// This code is contributed by Sam007
?>
JavaScript
<script>
// Javascript program to fill an
// array with frequencies.
// Fills arr[] with frequencies of elements
function fillWithFreq(arr, n)
{
let temp = new Array(n);
for(let i = 0; i < n; i++)
temp[i] = 0;
// Fill temp with frequencies
for (let i = 0; i < n; i++)
temp[arr[i]] += 1;
// Copy temp to array
for (let i = 0; i < n; i++)
arr[i] = temp[i];
}
let arr = [5, 2, 3, 4, 5, 5, 4, 5, 6, 7];
let n = arr.length;
// Function calling
fillWithFreq(arr, n);
for (let i = 0; i < n; i++)
document.write(arr[i] + " ");
</script>
Output0 0 1 1 2 4 1 1 0 0
Time Complexity : O(n)
Auxiliary Space : O(n)
Similar Reads
Find the frequency of each element in a sorted array Given a sorted array, arr[] consisting of N integers, the task is to find the frequencies of each array element. Examples: Input: arr[] = {1, 1, 1, 2, 3, 3, 5, 5, 8, 8, 8, 9, 9, 10} Output: Frequency of 1 is: 3 Frequency of 2 is: 1 Frequency of 3 is: 2 Frequency of 5 is: 2 Frequency of 8 is: 3 Frequ
10 min read
Replace every elements in the array by its frequency in the array Given an array of integers, replace every element by its frequency in the array. Examples: Input : arr[] = { 1, 2, 5, 2, 2, 5 } Output : 1 3 2 3 3 2 Input : arr[] = { 4 5 4 5 6 6 6 } Output : 2 2 2 2 3 3 3 Approach: Take a hash map, which will store the frequency of all the elements in the array.Now
5 min read
Find array elements with frequencies in range [l , r] Given an array of integers, find the elements from the array whose frequency lies in the range [l, r]. Examples: Input : arr[] = { 1, 2, 3, 3, 2, 2, 5 } l = 2, r = 3 Output : 2 3 3 2 2 Approach : Take a hash map, which will store the frequency of all the elements in the array.Now, traverse once agai
9 min read
Find frequency of each element in a limited range array in less than O(n) time Given a sorted array arr[] of positive integers, the task is to find the frequency for each element in the array. Assume all elements in the array are less than some constant MNote: Do this without traversing the complete array. i.e. expected time complexity is less than O(n)Examples: Input: arr[] =
10 min read
Maximum element in an array which is equal to its frequency Given an array of integers arr[] of size N, the task is to find the maximum element in the array whose frequency equals to it's value Examples: Input: arr[] = {3, 2, 2, 3, 4, 3} Output: 3 Frequency of element 2 is 2 Frequency of element 3 is 3 Frequency of element 4 is 1 2 and 3 are elements which h
11 min read