# Count the elements having frequency equal to its value

Here given code implementation process.

``````// Java Program
// Count the elements having frequency equal to its value
import java.util.HashMap;
public class Frequency
{
// Display array elements
public void printData(int[] arr, int n)
{
for (int i = 0; i < n; i++)
{
System.out.print("  " + arr[i]);
}
}
public void equalFrequency(int[] arr, int n)
{
int count = 0;
// Use to count element frequency
HashMap < Integer, Integer > record = new HashMap < Integer, Integer > ();
// iterate loop through by array size
for (int i = 0; i < n; i++)
{
// Check valid number under the size of array
if (arr[i] <= n && arr[i] > 0)
{
if (record.containsKey(arr[i]))
{
// increase frequency
record.put(arr[i], record.get(arr[i]) + 1);
}
else
{
// Add new element
record.put(arr[i], 1);
}
}
}
for (int key: record.keySet())
{
if (key == record.get(key))
{
// When element is equal to occurring frequency
count++;
}
}
// Display given array elements
printData(arr, n);
// Display calculated result
System.out.print("\n Result : " + count);
}
public static void main(String[] args)
{
Frequency task = new Frequency();
// Given array of integer elements
int[] arr = {
5 , 8 , 1 , 5 , 7 , -2 , 2 , 4 , 17 , 6 , 2 , 5 , 3 , 3 , 5 , 5
};
// Get the number of element
int n = arr.length;
// 1 exist 1 times
// 2 exist 2 times
// 5 occurs 5 times
// Test
task.equalFrequency(arr, n);
}
}``````

#### Output

``````  5  8  1  5  7  -2  2  4  17  6  2  5  3  3  5  5
Result : 3``````
``````// Include header file
#include <iostream>
#include <unordered_map>
using namespace std;

// C++ Program
// Count the elements having frequency equal to its value

class Frequency
{
public:
// Display array elements
void printData(int arr[], int n)
{
for (int i = 0; i < n; i++)
{
cout << "  " << arr[i];
}
}
void equalFrequency(int arr[], int n)
{
int count = 0;
// Use to count element frequency
unordered_map < int, int > record ;
// iterate loop through by array size
for (int i = 0; i < n; i++)
{
// Check valid number under the size of array
if (arr[i] <= n && arr[i] > 0)
{
if (record.find(arr[i]) != record.end())
{
// increase frequency
record[arr[i]] = record[arr[i]] + 1;
}
else
{
// Add new element
record[arr[i]] = 1;
}
}
}
for (auto &info: record)
{
if (info.first == info.second)
{
// When element is equal to occurring frequency
count++;
}
}
// Display given array elements
this->printData(arr, n);
// Display calculated result
cout << "\n Result : " << count;
}
};
int main()
{
Frequency task = Frequency();
// Given array of integer elements
int arr[] = {
5 , 8 , 1 , 5 , 7 , -2 , 2 , 4 , 17 , 6 , 2 , 5 , 3 , 3 , 5 , 5
};
// Get the number of element
int n = sizeof(arr) / sizeof(arr[0]);
// 1 exist 1 times
// 2 exist 2 times
// 5 occurs 5 times
// Test
task.equalFrequency(arr, n);
return 0;
}``````

#### Output

``````  5  8  1  5  7  -2  2  4  17  6  2  5  3  3  5  5
Result : 3``````
``````// Include namespace system
using System;
using System.Collections.Generic;
// C# Program
// Count the elements having frequency equal to its value
public class Frequency
{
// Display array elements
public void printData(int[] arr, int n)
{
for (int i = 0; i < n; i++)
{
Console.Write("  " + arr[i]);
}
}
public void equalFrequency(int[] arr, int n)
{
int count = 0;
// Use to count element frequency
Dictionary < int, int > record = new Dictionary < int, int > ();
// iterate loop through by array size
for (int i = 0; i < n; i++)
{
// Check valid number under the size of array
if (arr[i] <= n && arr[i] > 0)
{
if (record.ContainsKey(arr[i]))
{
// increase frequency
record[arr[i]] = record[arr[i]] + 1;
}
else
{
// Add new element
record.Add(arr[i], 1);
}
}
}
foreach(KeyValuePair < int, int > info in record)
{
if (info.Key == info.Value)
{
// When element is equal to occurring frequency
count++;
}
}
// Display given array elements
printData(arr, n);
// Display calculated result
Console.Write("\n Result : " + count);
}
public static void Main(String[] args)
{
Frequency task = new Frequency();
// Given array of integer elements
int[] arr = {
5 , 8 , 1 , 5 , 7 , -2 , 2 , 4 , 17 , 6 , 2 , 5 , 3 , 3 , 5 , 5
};
// Get the number of element
int n = arr.Length;
// 1 exist 1 times
// 2 exist 2 times
// 5 occurs 5 times
// Test
task.equalFrequency(arr, n);
}
}``````

#### Output

``````  5  8  1  5  7  -2  2  4  17  6  2  5  3  3  5  5
Result : 3``````
``````<?php
// Php Program
// Count the elements having frequency equal to its value
class Frequency
{
// Display array elements
public	function printData( & \$arr, \$n)
{
for (\$i = 0; \$i < \$n; \$i++)
{
echo "  ". \$arr[\$i];
}
}
public	function equalFrequency( & \$arr, \$n)
{
\$count = 0;
// Use to count element frequency
\$record = array();
// iterate loop through by array size
for (\$i = 0; \$i < \$n; \$i++)
{
// Check valid number under the size of array
if (\$arr[\$i] <= \$n && \$arr[\$i] > 0)
{
if (array_key_exists(\$arr[\$i], \$record))
{ // increase frequency
\$record[\$arr[\$i]] = \$record[\$arr[\$i]] + 1;
}
else
{ // Add new element
\$record[\$arr[\$i]] = 1;
}
}
}
foreach(\$record as \$key => \$value)
{
if (\$key == \$value)
{
// When element is equal to occurring frequency
\$count++;
}
}
// Display given array elements
\$this->printData(\$arr, \$n);
// Display calculated result
echo "\n Result : ". \$count;
}
}

function main()
{
\$task = new Frequency();
// Given array of integer elements
\$arr = array(5, 8, 1, 5, 7, -2, 2, 4, 17, 6, 2, 5, 3, 3, 5, 5);
// Get the number of element
\$n = count(\$arr);
\$task->equalFrequency(\$arr, \$n);
}
main();``````

#### Output

``````  5  8  1  5  7  -2  2  4  17  6  2  5  3  3  5  5
Result : 3``````
``````// Node Js Program
// Count the elements having frequency equal to its value
class Frequency
{
// Display array elements
printData(arr, n)
{
for (var i = 0; i < n; i++)
{
process.stdout.write("  " + arr[i]);
}
}
equalFrequency(arr, n)
{
var count = 0;
// Use to count element frequency
var record = new Map();
// iterate loop through by array size
for (var i = 0; i < n; i++)
{
// Check valid number under the size of array
if (arr[i] <= n && arr[i] > 0)
{
if (record.has(arr[i]))
{
// increase frequency
record.set(arr[i], record.get(arr[i]) + 1);
}
else
{
// Add new element
record.set(arr[i], 1);
}
}
}
for (let [key, value] of record)
{
if (key == value)
{
// When element is equal to occurring frequency
count++;
}
}
// Display given array elements
this.printData(arr, n);
// Display calculated result
process.stdout.write("\n Result : " + count);
}
}

function main()
{
var task = new Frequency();
// Given array of integer elements
var arr = [5, 8, 1, 5, 7, -2, 2, 4, 17, 6, 2, 5, 3, 3, 5, 5];
// Get the number of element
var n = arr.length;
// 1 exist 1 times
// 2 exist 2 times
// 5 occurs 5 times
// Test
task.equalFrequency(arr, n);
}
main();``````

#### Output

``````  5  8  1  5  7  -2  2  4  17  6  2  5  3  3  5  5
Result : 3``````
``````#  Python 3 Program
#  Count the elements having frequency equal to its value
class Frequency :
#  Display list elements
def printData(self, arr, n) :
i = 0
while (i < n) :
print(" ", arr[i], end = "")
i += 1

def equalFrequency(self, arr, n) :
count = 0
#  Use to count element frequency
record = dict()
i = 0
#  iterate loop through by list size
while (i < n) :
#  Check valid number under the size of list
if (arr[i] <= n and arr[i] > 0) :
if (arr[i] in record.keys()) :
#  increase frequency
record[arr[i]] = record.get(arr[i]) + 1
else :
#  Add new element
record[arr[i]] = 1

i += 1

for key, value in record.items() :
if (key == value) :
#  When element is equal to occurring frequency
count += 1

#  Display given list elements
self.printData(arr, n)
#  Display calculated result
print("\n Result : ", count, end = "")

def main() :
task = Frequency()
#  Given list of integer elements
arr = [5, 8, 1, 5, 7, -2, 2, 4, 17, 6, 2, 5, 3, 3, 5, 5]
#  Get the number of element
n = len(arr)
#  1 exist 1 times
#  2 exist 2 times
#  5 occurs 5 times
#  Test
task.equalFrequency(arr, n)

if __name__ == "__main__": main()``````

#### Output

``````  5  8  1  5  7  -2  2  4  17  6  2  5  3  3  5  5
Result :  3``````
``````import scala.collection.mutable._;
// Scala Program
// Count the elements having frequency equal to its value
class Frequency
{
// Display array elements
def printData(arr: Array[Int], n: Int): Unit = {
var i: Int = 0;
while (i < n)
{
print("  " + arr(i));
i += 1;
}
}
def equalFrequency(arr: Array[Int], n: Int): Unit = {
var count: Int = 0;
// Use to count element frequency
var record = Map[Int, Int]();
var i: Int = 0;
// iterate loop through by array size
while (i < n)
{
// Check valid number under the size of array
if (arr(i) <= n && arr(i) > 0)
{
if (record.contains(arr(i)))
{
// increase frequency
record.addOne(arr(i), record.get(arr(i)).get + 1);
}
else
{
// Add new element
record.addOne(arr(i), 1);
}
}
i += 1;
}
for ((k, v) <- record)
{
if (k == v)
{
// When element is equal to occurring frequency
count += 1;
}
}
// Display given array elements
this.printData(arr, n);
// Display calculated result
print("\n Result : " + count);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Frequency = new Frequency();
// Given array of integer elements
var arr: Array[Int] = Array(5, 8, 1, 5, 7, -2, 2, 4, 17, 6, 2, 5, 3, 3, 5, 5);
// Get the number of element
var n: Int = arr.length;
// 1 exist 1 times
// 2 exist 2 times
// 5 occurs 5 times
// Test
task.equalFrequency(arr, n);
}
}``````

#### Output

``````  5  8  1  5  7  -2  2  4  17  6  2  5  3  3  5  5
Result : 3``````
``````#  Ruby Program
#  Count the elements having frequency equal to its value
class Frequency
#  Display array elements
def printData(arr, n)
i = 0
while (i < n)
print("  ", arr[i])
i += 1
end

end

def equalFrequency(arr, n)
count = 0
#  Use to count element frequency
record = Hash.new
i = 0
#  iterate loop through by array size
while (i < n)
#  Check valid number under the size of array
if (arr[i] <= n && arr[i] > 0)
if (record.key?(arr[i]))
record[arr[i]] = record[arr[i]] + 1
else
record[arr[i]] = 1
end

end

i += 1
end

record.each { | key, value |
if (key == value)
#  When element is equal to occurring frequency
count += 1
end
}
#  Display given array elements
self.printData(arr, n)
#  Display calculated result
print("\n Result : ", count)
end

end

def main()
task = Frequency.new()
#  Given array of integer elements
arr = [5, 8, 1, 5, 7, -2, 2, 4, 17, 6, 2, 5, 3, 3, 5, 5]
#  Get the number of element
n = arr.length
#  1 exist 1 times
#  2 exist 2 times
#  5 occurs 5 times
#  Test
task.equalFrequency(arr, n)
end

main()``````

#### Output

``````  5  8  1  5  7  -2  2  4  17  6  2  5  3  3  5  5
Result : 3``````
``````import Foundation
// Swift 4 Program
// Count the elements having frequency equal to its value
class Frequency
{
// Display array elements
func printData(_ arr: [Int], _ n: Int)
{
var i: Int = 0;
while (i < n)
{
print("  ", arr[i], terminator: "");
i += 1;
}
}
func equalFrequency(_ arr: [Int], _ n: Int)
{
var count: Int = 0;
// Use to count element frequency
var record = [Int: Int]();
var i: Int = 0;
// iterate loop through by array size
while (i < n)
{
// Check valid number under the size of array
if (arr[i] <= n && arr[i] > 0)
{
if (record.keys.contains(arr[i]))
{
// increase frequency
record[arr[i]] = record[arr[i]]! + 1;
}
else
{
// Add new element
record[arr[i]] = 1;
}
}
i += 1;
}
for (key, value) in record
{
if (key == value)
{
// When element is equal to occurring frequency
count += 1;
}
}
// Display given array elements
self.printData(arr, n);
// Display calculated result
print("\n Result : ", count, terminator: "");
}
}
func main()
{
let task: Frequency = Frequency();
// Given array of integer elements
let arr: [Int] = [5, 8, 1, 5, 7, -2, 2, 4, 17, 6, 2, 5, 3, 3, 5, 5];
// Get the number of element
let n: Int = arr.count;
// 1 exist 1 times
// 2 exist 2 times
// 5 occurs 5 times
// Test
task.equalFrequency(arr, n);
}
main();``````

#### Output

``````   5   8   1   5   7   -2   2   4   17   6   2   5   3   3   5   5
Result :  3``````
``````// Kotlin Program
// Count the elements having frequency equal to its value
class Frequency
{
// Display array elements
fun printData(arr: Array < Int > , n: Int): Unit
{
var i: Int = 0;
while (i < n)
{
print("  " + arr[i]);
i += 1;
}
}
fun equalFrequency(arr: Array < Int > , n: Int): Unit
{
var count: Int = 0;
// Use to count element frequency
var record  = mutableMapOf < Int , Int > ();
var i: Int = 0;
// iterate loop through by array size
while (i < n)
{
// Check valid number under the size of array
if (arr[i] <= n && arr[i] > 0)
{
if (record.containsKey(arr[i]))
{
// increase frequency
record.put(arr[i], record.getValue(arr[i]) + 1);
}
else
{
// Add new element
record.put(arr[i], 1);
}
}
i += 1;
}
for (key in record.keys)
{
if (key == record.getValue(key))
{
// When element is equal to occurring frequency
count += 1;
}
}
// Display given array elements
this.printData(arr, n);
// Display calculated result
print("\n Result : " + count);
}
}
fun main(args: Array < String > ): Unit
{
var task: Frequency = Frequency();
// Given array of integer elements
var arr: Array < Int > =
arrayOf(5, 8, 1, 5, 7, -2, 2, 4, 17, 6, 2, 5, 3, 3, 5, 5);
// Get the number of element
var n: Int = arr.count();
// 1 exist 1 times
// 2 exist 2 times
// 5 occurs 5 times
// Test
task.equalFrequency(arr, n);
}``````

#### Output

``````  5  8  1  5  7  -2  2  4  17  6  2  5  3  3  5  5
Result : 3``````

## 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

© 2022, kalkicode.com, All rights reserved