# Find the frequency of each element in an array in kotlin

Kotlin program for Find the frequency of each element in an array. Here problem description and other solutions.

``````/*
Kotlin program
Count frequency of each element in array
*/
class Occurrence
{
// Function which is display array elements
fun display(arr: Array < Int > ): Unit
{
var i: Int = 0;
while (i < arr.count())
{
print("  " + arr[i]);
i += 1;
}
print("\n");
}
// Count occurrence of given array
fun frequency(arr: Array < Int > ): Unit
{
// Display given array
this.display(arr);
// Create a empty map
var map: HashMap < Int, Int > = HashMap < Int, Int > ();
var i: Int = 0;
while (i < arr.count())
{
if (map.containsKey(arr[i]))
{
// When key exists then update value
map.put(arr[i], map.getValue(arr[i]) + 1);
}
else
{
// Add new element
map.put(arr[i], 1);
}
i += 1;
}
println("  Occurrence ");
//  Display calculated result
for ((key, value) in map)
{
println("  " + key + "  :  " + value);
}
}
}
fun main(args: Array < String > ): Unit
{
val task: Occurrence = Occurrence();
// Array element
val arr: Array < Int > = arrayOf(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``````

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