Largest element in the array that is repeated exactly K times

Here given code implementation process.

import java.util.HashMap;
/*
    Java program for
    Largest element in the array that is repeated exactly K times
*/
public class Occurrence
{
	public void maxRepeatedK(int[] arr, int n, int k)
	{
		if (k < 0)
		{
			return;
		}
		HashMap < Integer, Integer > record = 
          new HashMap < Integer, Integer > ();
		// Auxiliary variable
		int result = 0;
		int count = 0;
		// Count frequency of array elements
		for (int i = 0; i < n; ++i)
		{
			if (record.containsKey(arr[i]))
			{
				// Increase the frequency
				record.put(arr[i], record.get(arr[i]) + 1);
			}
			else
			{
				// Add new element
				record.put(arr[i], 1);
			}
		}
		for (int v: record.keySet())
		{
			if (record.get(v) == k)
			{
				if (count == 0)
				{
					// First most occuring element
					result = v;
				}
				else if (result < v)
				{
					// Change most occuring element
					result = v;
				}
				count++;
			}
		}
		System.out.println(" Given K  : " + k);
		System.out.print(" Result   : ");
		if (count == 0)
		{
			System.out.print(" None \n");
		}
		else
		{
			// Display calculated result
			System.out.println(result);
		}
	}
	public static void main(String[] args)
	{
		Occurrence task = new Occurrence();
		int[] arr = {
			5 , 4 , 4 , 6 , 6 , 5 , 9 , 2 , 2 , 9 , 9
		};
		int n = arr.length;
		int k = 2;
		/*
		    k = 2 
		    (Element which contains k times)
		    
		    [5, 4, 6, 2] 
		    -----------
		    Max is 6
		*/
		task.maxRepeatedK(arr, n, k);
	}
}

Output

 Given K  : 2
 Result   : 6
// Include header file
#include <iostream>
#include <unordered_map>

using namespace std;
/*
    C++ program for
    Largest element in the array that is repeated exactly K times
*/
class Occurrence
{
	public: void maxRepeatedK(int arr[], int n, int k)
	{
		if (k < 0)
		{
			return;
		}
		unordered_map < int, int > record;
		// Auxiliary variable
		int result = 0;
		int count = 0;
		// Count frequency of array elements
		for (int i = 0; i < n; ++i)
		{
			if (record.find(arr[i]) != record.end())
			{
				// Increase the frequency
				record[arr[i]] = record[arr[i]] + 1;
			}
			else
			{
				// Add new element
				record[arr[i]] = 1;
			}
		}
		for (auto &v: record)
		{
			if (record[v.first] == k)
			{
				if (count == 0)
				{
					// First most occuring element
					result = v.first;
				}
				else if (result < v.first)
				{
					// Change most occuring element
					result = v.first;
				}
				count++;
			}
		}
		cout << " Given K  : " << k << endl;
		cout << " Result   : ";
		if (count == 0)
		{
			cout << " None \n";
		}
		else
		{
			// Display calculated result
			cout << result << endl;
		}
	}
};
int main()
{
	Occurrence *task = new Occurrence();
	int arr[] = {
		5 , 4 , 4 , 6 , 6 , 5 , 9 , 2 , 2 , 9 , 9
	};
	int n = sizeof(arr) / sizeof(arr[0]);
	int k = 2;
	/*
	    k = 2 
	    (Element which contains k times)
	    
	    [5, 4, 6, 2] 
	    -----------
	    Max is 6
	*/
	task->maxRepeatedK(arr, n, k);
	return 0;
}

Output

 Given K  : 2
 Result   : 6
// Include namespace system
using System;
using System.Collections.Generic;
/*
    Csharp program for
    Largest element in the array that is repeated exactly K times
*/
public class Occurrence
{
	public void maxRepeatedK(int[] arr, int n, int k)
	{
		if (k < 0)
		{
			return;
		}
		Dictionary < int, int > record = 
          new Dictionary < int, int > ();
		// Auxiliary variable
		int result = 0;
		int count = 0;
		// Count frequency of array elements
		for (int i = 0; i < n; ++i)
		{
			if (record.ContainsKey(arr[i]))
			{
				// Increase the frequency
				record[arr[i]] = record[arr[i]] + 1;
			}
			else
			{
				// Add new element
				record.Add(arr[i], 1);
			}
		}
		foreach(KeyValuePair < int, int > v in record)
		{
			if (v.Value == k)
			{
				if (count == 0)
				{
					// First most occuring element
					result = v.Key;
				}
				else if (result < v.Key)
				{
					// Change most occuring element
					result = v.Key;
				}
				count++;
			}
		}
		Console.WriteLine(" Given K  : " + k);
		Console.Write(" Result   : ");
		if (count == 0)
		{
			Console.Write(" None \n");
		}
		else
		{
			// Display calculated result
			Console.WriteLine(result);
		}
	}
	public static void Main(String[] args)
	{
		Occurrence task = new Occurrence();
		int[] arr = {
			5 , 4 , 4 , 6 , 6 , 5 , 9 , 2 , 2 , 9 , 9
		};
		int n = arr.Length;
		int k = 2;
		/*
		    k = 2 
		    (Element which contains k times)
		    
		    [5, 4, 6, 2] 
		    -----------
		    Max is 6
		*/
		task.maxRepeatedK(arr, n, k);
	}
}

Output

 Given K  : 2
 Result   : 6
package main
import "fmt"
/*
    Go program for
    Largest element in the array that is 
    repeated exactly K times
*/

func maxRepeatedK(arr[] int, n int, k int) {
	if k < 0 {
		return
	}
	var record = make(map[int] int)
	// Auxiliary variable
	var result int = 0
	var count int = 0
	// Count frequency of array elements
	for i := 0 ; i < n ; i++ {
		if _, found := record[arr[i]] ; found {
			// Increase the frequency
			record[arr[i]] = record[arr[i]] + 1
		} else {
			// Add new element
			record[arr[i]] = 1
		}
	}
	for key, v := range record {
		if v == k {
			if count == 0 {
				// First most occuring element
				result = key
			} else if result < key {
				// Change most occuring element
				result = key
			}
			count++
		}
	}
	fmt.Println(" Given K  : ", k)
	fmt.Print(" Result   : ")
	if count == 0 {
		fmt.Print(" None \n")
	} else {
		// Display calculated result
		fmt.Println(result)
	}
}
func main() {
	
	var arr = [] int {5, 4, 4, 6 , 6,  5 , 9, 2, 2, 9, 9}
	var n int = len(arr)
	var k int = 2
	/*
	    k = 2 
	    (Element which contains k times)
	    
	    [5, 4, 6, 2] 
	    -----------
	    Max is 6
	*/
	maxRepeatedK(arr, n, k)
}

Output

 Given K  : 2
 Result   : 6
<?php
/*
    Php program for
    Largest element in the array that is repeated exactly K times
*/
class Occurrence
{
	public	function maxRepeatedK($arr, $n, $k)
	{
		if ($k < 0)
		{
			return;
		}
		$record = array();
		// Auxiliary variable
		$result = 0;
		$count = 0;
		// Count frequency of array elements
		for ($i = 0; $i < $n; ++$i)
		{
			if (array_key_exists($arr[$i], $record))
			{
				// Increase the frequency
				$record[$arr[$i]] = $record[$arr[$i]] + 1;
			}
			else
			{
				// Add new element
				$record[$arr[$i]] = 1;
			}
		}
		foreach($record as $key => $value)
		{
			if ($value == $k)
			{
				if ($count == 0)
				{
					// First most occuring element
					$result = $key;
				}
				else if ($result < $key)
				{
					// Change most occuring element
					$result = $key;
				}
				$count++;
			}
		}
		echo(" Given K  : ".$k."\n");
		echo(" Result   : ");
		if ($count == 0)
		{
			echo(" None \n");
		}
		else
		{
			// Display calculated result
			echo($result."\n");
		}
	}
}

function main()
{
	$task = new Occurrence();
	$arr = array(5, 4, 4, 6, 6, 5, 9, 2, 2, 9, 9);
	$n = count($arr);
	$k = 2;
	/*
	    k = 2 
	    (Element which contains k times)
	    
	    [5, 4, 6, 2] 
	    -----------
	    Max is 6
	*/
	$task->maxRepeatedK($arr, $n, $k);
}
main();

Output

 Given K  : 2
 Result   : 6
/*
    Node JS program for
    Largest element in the array that is repeated exactly K times
*/
class Occurrence
{
	maxRepeatedK(arr, n, k)
	{
		if (k < 0)
		{
			return;
		}
		var record = new Map();
		// Auxiliary variable
		var result = 0;
		var count = 0;
		// Count frequency of array elements
		for (var i = 0; i < n; ++i)
		{
			if (record.has(arr[i]))
			{
				// Increase the 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 (value == k)
			{
				if (count == 0)
				{
					// First most occuring element
					result = key;
				}
				else if (result < key)
				{
					// Change most occuring element
					result = key;
				}
				count++;
			}
		}
		console.log(" Given K  : " + k);
		process.stdout.write(" Result   : ");
		if (count == 0)
		{
			process.stdout.write(" None \n");
		}
		else
		{
			// Display calculated result
			console.log(result);
		}
	}
}

function main()
{
	var task = new Occurrence();
	var arr = [5, 4, 4, 6, 6, 5, 9, 2, 2, 9, 9];
	var n = arr.length;
	var k = 2;
	/*
	    k = 2 
	    (Element which contains k times)
	    
	    [5, 4, 6, 2] 
	    -----------
	    Max is 6
	*/
	task.maxRepeatedK(arr, n, k);
}
main();

Output

 Given K  : 2
 Result   : 6
#    Python 3 program for
#    Largest element in the array that is repeated exactly K times
class Occurrence :
	def maxRepeatedK(self, arr, n, k) :
		if (k < 0) :
			return
		
		record = dict()
		#  Auxiliary variable
		result = 0
		count = 0
		i = 0
		#  Count frequency of list elements
		while (i < n) :
			if ((arr[i] in record.keys())) :
				#  Increase the 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 (value == k) :
				if (count == 0) :
					#  First most occuring element
					result = key
				elif (result < key) :
					#  Change most occuring element
					result = key
				
				count += 1
			
		
		print(" Given K :", k)
		print(" Result  : ", end = "")
		if (count == 0) :
			print(" None ")
		else :
			#  Display calculated result
			print(result)
		
	

def main() :
	task = Occurrence()
	arr = [5, 4, 4, 6, 6, 5, 9, 2, 2, 9, 9]
	n = len(arr)
	k = 2
	#    k = 2 
	#    (Element which contains k times)
	#    [5, 4, 6, 2] 
	#    -----------
	#    Max is 6
	task.maxRepeatedK(arr, n, k)

if __name__ == "__main__": main()

Output

 Given K : 2
 Result  : 6
#    Ruby program for
#    Largest element in the array that is repeated exactly K times
class Occurrence 
	def maxRepeatedK(arr, n, k) 
		if (k < 0) 
			return
		end

		record = Hash.new()
		#  Auxiliary variable
		result = 0
		count = 0
		i = 0
		#  Count frequency of array elements
		while (i < n) 
			if (record.key?(arr[i])) 
				#  Increase the frequency
				record[arr[i]] = record[arr[i]] + 1
			else
 
				#  Add new element
				record[arr[i]] = 1
			end

			i += 1
		end

		record.each { | key, value |
			if (value == k) 
				if (count == 0) 
					#  First most occuring element
					result = key
				elsif (result < key) 
					#  Change most occuring element
					result = key
				end

				count += 1
			end

		}
		print(" Given K  : ", k, "\n")
		print(" Result   : ")
		if (count == 0) 
			print(" None \n")
		else
 
			#  Display calculated result
			print(result, "\n")
		end

	end

end

def main() 
	task = Occurrence.new()
	arr = [5, 4, 4, 6, 6, 5, 9, 2, 2, 9, 9]
	n = arr.length
	k = 2
	#    k = 2 
	#    (Element which contains k times)
	#    [5, 4, 6, 2] 
	#    -----------
	#    Max is 6
	task.maxRepeatedK(arr, n, k)
end

main()

Output

 Given K  : 2
 Result   : 6
import scala.collection.mutable._;
/*
    Scala program for
    Largest element in the array that is repeated exactly K times
*/
class Occurrence()
{
	def maxRepeatedK(arr: Array[Int], n: Int, k: Int): Unit = {
		if (k < 0)
		{
			return;
		}
		var record = new HashMap[Int, Int]();
		// Auxiliary variable
		var result: Int = 0;
		var count: Int = 0;
		var i: Int = 0;
		// Count frequency of array elements
		while (i < n)
		{
			if (record.contains(arr(i)))
			{
				// Increase the frequency
				record.addOne(arr(i), record.get(arr(i)).get + 1);
			}
			else
			{
				// Add new element
				record.addOne(arr(i), 1);
			}
			i += 1;
		}
		for ((key, value) <- record)
		{
			if (value == k)
			{
				if (count == 0)
				{
					// First most occuring element
					result = key;
				}
				else if (result < key)
				{
					// Change most occuring element
					result = key;
				}
				count += 1;
			}
		}
		println(" Given K  : " + k);
		print(" Result   : ");
		if (count == 0)
		{
			print(" None \n");
		}
		else
		{
			// Display calculated result
			println(result);
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Occurrence = new Occurrence();
		var arr: Array[Int] = Array(
          5, 4, 4, 6, 6, 5, 9, 2, 2, 9, 9
        );
		var n: Int = arr.length;
		var k: Int = 2;
		/*
		    k = 2 
		    (Element which contains k times)
		    
		    [5, 4, 6, 2] 
		    -----------
		    Max is 6
		*/
		task.maxRepeatedK(arr, n, k);
	}
}

Output

 Given K  : 2
 Result   : 6
import Foundation;
/*
    Swift 4 program for
    Largest element in the array that is repeated exactly K times
*/
class Occurrence
{
	func maxRepeatedK(_ arr: [Int], _ n: Int, _ k: Int)
	{
		if (k < 0)
		{
			return;
		}
		var record = [Int : Int]();
		// Auxiliary variable
		var result: Int = 0;
		var count: Int = 0;
		var i: Int = 0;
		// Count frequency of array elements
		while (i < n)
		{
			if (record.keys.contains(arr[i]))
			{
				// Increase the frequency
				record[arr[i]] = record[arr[i]]! + 1;
			}
			else
			{
				// Add new element
				record[arr[i]] = 1;
			}
			i += 1;
		}
		for (key, value) in record
		{
			if (value == k)
			{
				if (count == 0)
				{
					// First most occuring element
					result = key;
				}
				else if (result < key)
				{
					// Change most occuring element
					result = key;
				}
				count += 1;
			}
		}
		print(" Given K  : ", k);
		print(" Result   : ", terminator: "");
		if (count == 0)
		{
			print(" None ");
		}
		else
		{
			// Display calculated result
			print(result);
		}
	}
}
func main()
{
	let task: Occurrence = Occurrence();
	let arr: [Int] = [5, 4, 4, 6, 6, 5, 9, 2, 2, 9, 9];
	let n: Int = arr.count;
	let k: Int = 2;
	/*
	    k = 2 
	    (Element which contains k times)
	    
	    [5, 4, 6, 2] 
	    -----------
	    Max is 6
	*/
	task.maxRepeatedK(arr, n, k);
}
main();

Output

 Given K  :  2
 Result   : 6
/*
    Kotlin program for
    Largest element in the array that is repeated exactly K times
*/
class Occurrence
{
	fun maxRepeatedK(arr: Array < Int > , n: Int, k: Int): Unit
	{
		if (k < 0)
		{
			return;
		}
		var record = HashMap < Int, Int > ();
		// Auxiliary variable
		var result: Int = 0;
		var count: Int = 0;
		var i: Int = 0;
		// Count frequency of array elements
		while (i < n)
		{
			if (record.containsKey(arr[i]))
			{
				// Increase the frequency
				record.put(arr[i], record.getValue(arr[i]) + 1);
			}
			else
			{
				// Add new element
				record.put(arr[i], 1);
			}
			i += 1;
		}
		for ((key, value) in record)
		{
			if (value == k)
			{
				if (count == 0)
				{
					// First most occuring element
					result = key;
				}
				else if (result < key)
				{
					// Change most occuring element
					result = key;
				}
				count += 1;
			}
		}
		println(" Given K  : " + k);
		print(" Result   : ");
		if (count == 0)
		{
			print(" None \n");
		}
		else
		{
			// Display calculated result
			println(result);
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Occurrence = Occurrence();
	val arr: Array < Int > = arrayOf(
      5, 4, 4, 6, 6, 5, 9, 2, 2, 9, 9
    );
	val n: Int = arr.count();
	val k: Int = 2;
	/*
	    k = 2 
	    (Element which contains k times)
	    
	    [5, 4, 6, 2] 
	    -----------
	    Max is 6
	*/
	task.maxRepeatedK(arr, n, k);
}

Output

 Given K  : 2
 Result   : 6


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







© 2021, kalkicode.com, All rights reserved