# Find first element to occurring K times in array

Here given code implementation process.

```
import java.util.HashMap;
/*
Java program for
Find first element to occurring K times in array
*/
public class Occurrence
{
public void firstOccurrenceOfK(int[] arr, int n, int k)
{
if (k > n || k < 0)
{
return;
}
HashMap < Integer, Integer > record =
new HashMap < Integer, Integer > ();
// Count frequency of array elements
for (int i = 0; i < n; ++i)
{
if (record.containsKey(arr[i]))
{
record.put(arr[i], record.get(arr[i]) + 1);
}
else
{
record.put(arr[i], 1);
}
}
// Display given value K
System.out.print(" Given K : " + k);
System.out.print("\n Result : ");
for (int i = 0; i < n; ++i)
{
if (record.get(arr[i]) == k)
{
// We get first element which
// occurring k number times
System.out.println(arr[i]);
return;
}
}
// When result not exist
System.out.print(" None \n");
}
public static void main(String[] args)
{
Occurrence task = new Occurrence();
int[] arr = {
5 , 3 , 2 , 3 , 4 , 4 , 5 , 4 , 2 , 6 , 2 , 9
};
int n = arr.length;
int k = 3;
/*
arr = [5, 3, 2, 3, 4 , 4, 5, 4 , 2, 6, 2, 9]
k = 3
Element which occurring k times in array is
[2,4]
-------------
First is 2
*/
task.firstOccurrenceOfK(arr, n, k);
}
}
```

#### Output

```
Given K : 3
Result : 2
```

```
// Include header file
#include <iostream>
#include <unordered_map>
using namespace std;
/*
C++ program for
Find first element to occurring K times in array
*/
class Occurrence
{
public: void firstOccurrenceOfK(int arr[], int n, int k)
{
if (k > n || k < 0)
{
return;
}
unordered_map < int, int > record;
// Count frequency of array elements
for (int i = 0; i < n; ++i)
{
if (record.find(arr[i]) != record.end())
{
record[arr[i]] = record[arr[i]] + 1;
}
else
{
record[arr[i]] = 1;
}
}
// Display given value K
cout << " Given K : " << k;
cout << "\n Result : ";
for (int i = 0; i < n; ++i)
{
if (record[arr[i]] == k)
{
// We get first element which
// occurring k number times
cout << arr[i] << endl;
return;
}
}
// When result not exist
cout << " None \n";
}
};
int main()
{
Occurrence *task = new Occurrence();
int arr[] = {
5 , 3 , 2 , 3 , 4 , 4 , 5 , 4 , 2 , 6 , 2 , 9
};
int n = sizeof(arr) / sizeof(arr[0]);
int k = 3;
/*
arr = [5, 3, 2, 3, 4 , 4, 5, 4 , 2, 6, 2, 9]
k = 3
Element which occurring k times in array is
[2,4]
-------------
First is 2
*/
task->firstOccurrenceOfK(arr, n, k);
return 0;
}
```

#### Output

```
Given K : 3
Result : 2
```

```
// Include namespace system
using System;
using System.Collections.Generic;
/*
Csharp program for
Find first element to occurring K times in array
*/
public class Occurrence
{
public void firstOccurrenceOfK(int[] arr, int n, int k)
{
if (k > n || k < 0)
{
return;
}
Dictionary < int, int > record =
new Dictionary < int, int > ();
// Count frequency of array elements
for (int i = 0; i < n; ++i)
{
if (record.ContainsKey(arr[i]))
{
record[arr[i]] = record[arr[i]] + 1;
}
else
{
record.Add(arr[i], 1);
}
}
// Display given value K
Console.Write(" Given K : " + k);
Console.Write("\n Result : ");
for (int i = 0; i < n; ++i)
{
if (record[arr[i]] == k)
{
// We get first element which
// occurring k number times
Console.WriteLine(arr[i]);
return;
}
}
// When result not exist
Console.Write(" None \n");
}
public static void Main(String[] args)
{
Occurrence task = new Occurrence();
int[] arr = {
5 , 3 , 2 , 3 , 4 , 4 , 5 , 4 , 2 , 6 , 2 , 9
};
int n = arr.Length;
int k = 3;
/*
arr = [5, 3, 2, 3, 4 , 4, 5, 4 , 2, 6, 2, 9]
k = 3
Element which occurring k times in array is
[2,4]
-------------
First is 2
*/
task.firstOccurrenceOfK(arr, n, k);
}
}
```

#### Output

```
Given K : 3
Result : 2
```

```
package main
import "fmt"
/*
Go program for
Find first element to occurring K times in array
*/
func firstOccurrenceOfK(arr[] int, n int, k int) {
if k > n || k < 0 {
return
}
var record = make(map[int] int)
// Count frequency of array elements
for i := 0 ; i < n ; i++ {
if _, found := record[arr[i]] ; found {
record[arr[i]] = record[arr[i]] + 1
} else {
record[arr[i]] = 1
}
}
// Display given value K
fmt.Print(" Given K : ", k)
fmt.Print("\n Result : ")
for i := 0 ; i < n ; i++ {
if record[arr[i]] == k {
// We get first element which
// occurring k number times
fmt.Println(arr[i])
return
}
}
// When result not exist
fmt.Print(" None \n")
}
func main() {
var arr = [] int {5, 3, 2, 3, 4 , 4, 5, 4 , 2, 6, 2, 9}
var n int = len(arr)
var k int = 3
/*
arr = [5, 3, 2, 3, 4 , 4, 5, 4 , 2, 6, 2, 9]
k = 3
Element which occurring k times in array is
[2,4]
-------------
First is 2
*/
firstOccurrenceOfK(arr, n, k)
}
```

#### Output

```
Given K : 3
Result : 2
```

```
<?php
/*
Php program for
Find first element to occurring K times in array
*/
class Occurrence
{
public function firstOccurrenceOfK($arr, $n, $k)
{
if ($k > $n || $k < 0)
{
return;
}
$record = array();
// Count frequency of array elements
for ($i = 0; $i < $n; ++$i)
{
if (array_key_exists($arr[$i], $record))
{
$record[$arr[$i]] = $record[$arr[$i]] + 1;
}
else
{
$record[$arr[$i]] = 1;
}
}
// Display given value K
echo(" Given K : ".$k);
echo("\n Result : ");
for ($i = 0; $i < $n; ++$i)
{
if ($record[$arr[$i]] == $k)
{
// We get first element which
// occurring k number times
echo($arr[$i]."\n");
return;
}
}
// When result not exist
echo(" None \n");
}
}
function main()
{
$task = new Occurrence();
$arr = array(5, 3, 2, 3, 4, 4, 5, 4, 2, 6, 2, 9);
$n = count($arr);
$k = 3;
/*
arr = [5, 3, 2, 3, 4 , 4, 5, 4 , 2, 6, 2, 9]
k = 3
Element which occurring k times in array is
[2,4]
-------------
First is 2
*/
$task->firstOccurrenceOfK($arr, $n, $k);
}
main();
```

#### Output

```
Given K : 3
Result : 2
```

```
/*
Node JS program for
Find first element to occurring K times in array
*/
class Occurrence
{
firstOccurrenceOfK(arr, n, k)
{
if (k > n || k < 0)
{
return;
}
var record = new Map();
// Count frequency of array elements
for (var i = 0; i < n; ++i)
{
if (record.has(arr[i]))
{
record.set(arr[i], record.get(arr[i]) + 1);
}
else
{
record.set(arr[i], 1);
}
}
// Display given value K
process.stdout.write(" Given K : " + k);
process.stdout.write("\n Result : ");
for (var i = 0; i < n; ++i)
{
if (record.get(arr[i]) == k)
{
// We get first element which
// occurring k number times
console.log(arr[i]);
return;
}
}
// When result not exist
process.stdout.write(" None \n");
}
}
function main()
{
var task = new Occurrence();
var arr = [5, 3, 2, 3, 4, 4, 5, 4, 2, 6, 2, 9];
var n = arr.length;
var k = 3;
/*
arr = [5, 3, 2, 3, 4 , 4, 5, 4 , 2, 6, 2, 9]
k = 3
Element which occurring k times in array is
[2,4]
-------------
First is 2
*/
task.firstOccurrenceOfK(arr, n, k);
}
main();
```

#### Output

```
Given K : 3
Result : 2
```

```
# Python 3 program for
# Find first element to occurring K times in array
class Occurrence :
def firstOccurrenceOfK(self, arr, n, k) :
if (k > n or k < 0) :
return
record = dict()
i = 0
# Count frequency of list elements
while (i < n) :
if ((arr[i] in record.keys())) :
record[arr[i]] = record.get(arr[i]) + 1
else :
record[arr[i]] = 1
i += 1
# Display given value K
print(" Given K :", k, end = "")
print("\n Result : ", end = "")
i = 0
while (i < n) :
if (record.get(arr[i]) == k) :
# We get first element which
# occurring k number times
print(arr[i])
return
i += 1
# When result not exist
print(" None ")
def main() :
task = Occurrence()
arr = [5, 3, 2, 3, 4, 4, 5, 4, 2, 6, 2, 9]
n = len(arr)
k = 3
# arr = [5, 3, 2, 3, 4 , 4, 5, 4 , 2, 6, 2, 9]
# k = 3
# Element which occurring k times in list is
# [2,4]
# -------------
# First is 2
task.firstOccurrenceOfK(arr, n, k)
if __name__ == "__main__": main()
```

#### Output

```
Given K : 3
Result : 2
```

```
# Ruby program for
# Find first element to occurring K times in array
class Occurrence
def firstOccurrenceOfK(arr, n, k)
if (k > n || k < 0)
return
end
record = Hash.new()
i = 0
# Count frequency of array elements
while (i < n)
if (record.key?(arr[i]))
record[arr[i]] = record[arr[i]] + 1
else
record[arr[i]] = 1
end
i += 1
end
# Display given value K
print(" Given K : ", k)
print("\n Result : ")
i = 0
while (i < n)
if (record[arr[i]] == k)
# We get first element which
# occurring k number times
print(arr[i], "\n")
return
end
i += 1
end
# When result not exist
print(" None \n")
end
end
def main()
task = Occurrence.new()
arr = [5, 3, 2, 3, 4, 4, 5, 4, 2, 6, 2, 9]
n = arr.length
k = 3
# arr = [5, 3, 2, 3, 4 , 4, 5, 4 , 2, 6, 2, 9]
# k = 3
# Element which occurring k times in array is
# [2,4]
# -------------
# First is 2
task.firstOccurrenceOfK(arr, n, k)
end
main()
```

#### Output

```
Given K : 3
Result : 2
```

```
import scala.collection.mutable._;
/*
Scala program for
Find first element to occurring K times in array
*/
class Occurrence()
{
def firstOccurrenceOfK(arr: Array[Int],
n: Int, k: Int): Unit = {
if (k > n || k < 0)
{
return;
}
var record: HashMap[Int, Int] =
new HashMap[Int, Int]();
var i: Int = 0;
// Count frequency of array elements
while (i < n)
{
if (record.contains(arr(i)))
{
record.addOne(arr(i), record.get(arr(i)).get + 1);
}
else
{
record.addOne(arr(i), 1);
}
i += 1;
}
// Display given value K
print(" Given K : " + k);
print("\n Result : ");
i = 0;
while (i < n)
{
if (record.get(arr(i)).get == k)
{
// We get first element which
// occurring k number times
println(arr(i));
return;
}
i += 1;
}
// When result not exist
print(" None \n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Occurrence = new Occurrence();
var arr: Array[Int] = Array(5, 3, 2, 3, 4, 4, 5, 4, 2, 6, 2, 9);
var n: Int = arr.length;
var k: Int = 3;
/*
arr = [5, 3, 2, 3, 4 , 4, 5, 4 , 2, 6, 2, 9]
k = 3
Element which occurring k times in array is
[2,4]
-------------
First is 2
*/
task.firstOccurrenceOfK(arr, n, k);
}
}
```

#### Output

```
Given K : 3
Result : 2
```

```
import Foundation;
/*
Swift 4 program for
Find first element to occurring K times in array
*/
class Occurrence
{
func firstOccurrenceOfK(_ arr: [Int], _ n: Int, _ k: Int)
{
if (k > n || k < 0)
{
return;
}
var record = [Int : Int]();
var i: Int = 0;
// Count frequency of array elements
while (i < n)
{
if (record.keys.contains(arr[i]))
{
record[arr[i]] = record[arr[i]]! + 1;
}
else
{
record[arr[i]] = 1;
}
i += 1;
}
// Display given value K
print(" Given K : ", k, terminator: "");
print("\n Result : ", terminator: "");
i = 0;
while (i < n)
{
if (record[arr[i]] == k)
{
// We get first element which
// occurring k number times
print(arr[i]);
return;
}
i += 1;
}
// When result not exist
print(" None ");
}
}
func main()
{
let task: Occurrence = Occurrence();
let arr: [Int] = [5, 3, 2, 3, 4, 4, 5, 4, 2, 6, 2, 9];
let n: Int = arr.count;
let k: Int = 3;
/*
arr = [5, 3, 2, 3, 4 , 4, 5, 4 , 2, 6, 2, 9]
k = 3
Element which occurring k times in array is
[2,4]
-------------
First is 2
*/
task.firstOccurrenceOfK(arr, n, k);
}
main();
```

#### Output

```
Given K : 3
Result : 2
```

```
/*
Kotlin program for
Find first element to occurring K times in array
*/
class Occurrence
{
fun firstOccurrenceOfK(arr: Array < Int > , n: Int, k: Int): Unit
{
if (k > n || k < 0)
{
return;
}
val record: HashMap < Int, Int > = HashMap < Int, Int > ();
var i: Int = 0;
// Count frequency of array elements
while (i < n)
{
if (record.containsKey(arr[i]))
{
record.put(arr[i], record.getValue(arr[i]) + 1);
}
else
{
record.put(arr[i], 1);
}
i += 1;
}
// Display given value K
print(" Given K : " + k);
print("\n Result : ");
i = 0;
while (i < n)
{
if (record.getValue(arr[i]) == k)
{
// We get first element which
// occurring k number times
println(arr[i]);
return;
}
i += 1;
}
// When result not exist
print(" None \n");
}
}
fun main(args: Array < String > ): Unit
{
val task: Occurrence = Occurrence();
val arr: Array < Int > =
arrayOf(5, 3, 2, 3, 4, 4, 5, 4, 2, 6, 2, 9);
val n: Int = arr.count();
val k: Int = 3;
/*
arr = [5, 3, 2, 3, 4 , 4, 5, 4 , 2, 6, 2, 9]
k = 3
Element which occurring k times in array is
[2,4]
-------------
First is 2
*/
task.firstOccurrenceOfK(arr, n, k);
}
```

#### Output

```
Given K : 3
Result : 2
```

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