Sum of all maximum frequency elements in matrix

Here given code implementation process.

// Java Program 
// Sum of all maximum frequency elements in matrix
import java.util.HashMap;
public class Frequency
{
	public void maxOccurrenceSum(int[][] matrix)
	{
		// Get the length
		int r = matrix.length;
		int c = matrix[0].length;
		// Use to count frequency
		HashMap < Integer, Integer > record = new HashMap < Integer, Integer > ();
		int max = 1;
		// Execute loop through by matrix row
		for (int i = 0; i < r; i++)
		{
			// Execute loop through by matrix column
			for (int j = 0; j < c; ++j)
			{
				if (record.containsKey(matrix[i][j]))
				{
					// increase element frequency
					record.put(matrix[i][j], record.get(matrix[i][j]) + 1);
					if (max < record.get(matrix[i][j]))
					{
						// Get new max occurrence frequency
						max = record.get(matrix[i][j]);
					}
				}
				else
				{
					// Add new element
					record.put(matrix[i][j], 1);
				}
			}
		}
		int sum = 0;
		// Finds the sum of max occurring frequency
		for (int key: record.keySet())
		{
			if (record.get(key) == max)
			{
				sum += key * record.get(key);
			}
		}
		// Display calculated result
		System.out.print(" Result " + sum);
	}
	public static void main(String[] args)
	{
		Frequency task = new Frequency();
		int[][] matrix = {
			{
				1 , 7 , -2 , 6 , 5
			} , 
          	{
				8 , 3 , 4 , 5 , 7
			} , 
          	{
				6 , 2 , 1 , 3 , -2
			} , 
          	{
				3 , 2 , 1 , -2 , 6
			}
		};
        // Test
		// (3+3+3) + (1+1+1) + (-2 + -2 + -2)
        // (6+6+6)
		task.maxOccurrenceSum(matrix);
	}
}

Output

 Result 24
// Include header file
#include <iostream>
#include <unordered_map>
using namespace std;
#define R 4
#define C 5

// C++ Program
// Sum of all maximum frequency elements in matrix
class Frequency
{
	public: void maxOccurrenceSum(int matrix[R][C])
	{
		
		// Use to count frequency
		unordered_map < int, int > record ;
		int max = 1;
		// Execute loop through by matrix row
		for (int i = 0; i < R; i++)
		{
			// Execute loop through by matrix column
			for (int j = 0; j < C; ++j)
			{
				if (record.find(matrix[i][j]) != record.end())
				{
					// increase element frequency
					record[matrix[i][j]] = record[matrix[i][j]] + 1;
					if (max < record[matrix[i][j]])
					{
						// Get new max occurrence frequency
						max = record[matrix[i][j]];
					}
				}
				else
				{
					// Add new element
					record[matrix[i][j]] = 1;
				}
			}
		}
		int sum = 0;
		for (auto &info: record)
		{
			if (info.second == max)
			{
				sum += info.first * info.second;
			}
		}
		cout << " Result " << sum;
	}
};
int main()
{
	Frequency task = Frequency();
	int matrix[R][C] = {
		{
			1 , 7 , -2 , 6 , 5
		} , {
			8 , 3 , 4 , 5 , 7
		} , {
			6 , 2 , 1 , 3 , -2
		} , {
			3 , 2 , 1 , -2 , 6
		}
	};
	// Test
	// (3+3+3) + (1+1+1) + (-2 + -2 + -2)
	// (6+6+6)
	task.maxOccurrenceSum(matrix);
	return 0;
}

Output

 Result 24
// Include namespace system
using System;
using System.Collections.Generic;

// C# Program
// Sum of all maximum frequency elements in matrix
public class Frequency
{
	public void maxOccurrenceSum(int[,] matrix)
	{
		// Get the length
		int r = matrix.GetLength(0);
		int c = matrix.GetLength(1);
		// Use to count frequency
		Dictionary < int, int > record = new Dictionary < int, int > ();
		int max = 1;
		// Execute loop through by matrix row
		for (int i = 0; i < r; i++)
		{
			// Execute loop through by matrix column
			for (int j = 0; j < c; ++j)
			{
				if (record.ContainsKey(matrix[i,j]))
				{
					// increase element frequency
					record[matrix[i,j]] = record[matrix[i,j]] + 1;
					if (max < record[matrix[i,j]])
					{
						// Get new max occurrence frequency
						max = record[matrix[i,j]];
					}
				}
				else
				{
					// Add new element
					record.Add(matrix[i,j], 1);
				}
			}
		}
		int sum = 0;
		foreach(KeyValuePair < int, int > info in record)
		{
			if (info.Value == max)
			{
				sum += info.Key * info.Value;
			}
		}
		// Display calculated result
		Console.Write(" Result " + sum);
	}
	public static void Main(String[] args)
	{
		Frequency task = new Frequency();
		int[,] matrix = {
			{
				1 , 7 , -2 , 6 , 5
			} , {
				8 , 3 , 4 , 5 , 7
			} , {
				6 , 2 , 1 , 3 , -2
			} , {
				3 , 2 , 1 , -2 , 6
			}
		};
		// Test
		// (3+3+3) + (1+1+1) + (-2 + -2 + -2)
		// (6+6+6)
		task.maxOccurrenceSum(matrix);
	}
}

Output

 Result 24
<?php
// Php Program
// Sum of all maximum frequency elements in matrix
class Frequency
{
	public	function maxOccurrenceSum( & $matrix)
	{
		// Get the length
		$r = count($matrix);
		$c = count($matrix[0]);
		// Use to count frequency
		$record = array();
		$max = 1;
		// Execute loop through by matrix row
		for ($i = 0; $i < $r; $i++)
		{
			// Execute loop through by matrix column
			for ($j = 0; $j < $c; ++$j)
			{
				if (array_key_exists($matrix[$i][$j], $record))
				{ // increase element frequency
					$record[$matrix[$i][$j]] = $record[$matrix[$i][$j]] + 1;
					if ($max < $record[$matrix[$i][$j]])
					{
						// Get new max occurrence frequency
						$max = $record[$matrix[$i][$j]];
					}
				}
				else
				{ // Add new element
					$record[$matrix[$i][$j]] = 1;
				}
			}
		}
		$sum = 0;
		foreach($record as $key => $value)
		{
			if ( $value == $max)
			{
				$sum += $key * $value;
			}
		}
		// Display calculated result
		echo " Result ".$sum;
	}
}

function main()
{
	$task = new Frequency();
	$matrix = array(
      array(1, 7, -2, 6, 5), 
      array(8, 3, 4, 5, 7), 
      array(6, 2, 1, 3, -2), 
      array(3, 2, 1, -2, 6)
    ); 
     // Test
	// (3+3+3) + (1+1+1) + (-2 + -2 + -2)
	// (6+6+6)
	$task->maxOccurrenceSum($matrix);
}
main();

Output

 Result 24
// Node Js Program
// Sum of all maximum frequency elements in matrix
class Frequency
{
	maxOccurrenceSum(matrix)
	{
		// Get the length
		var r = matrix.length;
		var c = matrix[0].length;
		// Use to count frequency
		var record = new Map();
		var max = 1;
		// Execute loop through by matrix row
		for (var i = 0; i < r; i++)
		{
			// Execute loop through by matrix column
			for (var j = 0; j < c; ++j)
			{
				if (record.has(matrix[i][j]))
				{
					// increase element frequency
					record.set(matrix[i][j], record.get(matrix[i][j]) + 1);
					if (max < record.get(matrix[i][j]))
					{
						// Get new max occurrence frequency
						max = record.get(matrix[i][j]);
					}
				}
				else
				{
					// Add new element
					record.set(matrix[i][j], 1);
				}
			}
		}
		var sum = 0;
		for (let [key, value] of record)
		{
			if (value == max)
			{
				sum += key * value;
			}
		}
		// Display calculated result
		process.stdout.write(" Result " + sum);
	}
}

function main()
{
	var task = new Frequency();
	var matrix = [
		[1, 7, -2, 6, 5] , 
        [8, 3, 4, 5, 7] , 
        [6, 2, 1, 3, -2] , 
        [3, 2, 1, -2, 6]
	];
	// Test
	// (3+3+3) + (1+1+1) + (-2 + -2 + -2)
	// (6+6+6)
	task.maxOccurrenceSum(matrix);
}
main();

Output

 Result 24
#  Python 3 Program
#  Sum of all maximum frequency elements in matrix
class Frequency :
	def maxOccurrenceSum(self, matrix) :
		#  Get the length
		r = len(matrix)
		c = len(matrix[0])
		#  Use to count frequency
		record = dict()
		max = 1
		#  Execute loop through by matrix row
		i = 0
		while (i < r) :
			#  Execute loop through by matrix column
			j = 0
			while (j < c) :
				if (matrix[i][j] in record.keys()) :
					#  increase element frequency
					record[matrix[i][j]] = record.get(matrix[i][j]) + 1
					if (max < record.get(matrix[i][j])) :
						#  Get new max occurrence frequency
						max = record.get(matrix[i][j])
					
				else :
					#  Add new element
					record[matrix[i][j]] = 1
				
				j += 1
			
			i += 1
		
		sum = 0
		for key, value in record.items() :
			if (record.get(key) == max) :
				sum += key * record.get(key)
		
		#  Display calculated result
		print(" Result ", sum, end = "")
	

def main() :
	task = Frequency()
	matrix = [
		[1, 7, -2, 6, 5] , 
        [8, 3, 4, 5, 7] , 
        [6, 2, 1, 3, -2] , 
        [3, 2, 1, -2, 6]
	] #  Test
	#  (3+3+3) + (1+1+1) + (-2 + -2 + -2)
	#  (6+6+6)
	task.maxOccurrenceSum(matrix)

if __name__ == "__main__": main()

Output

 Result  24
#  Ruby Program
#  Sum of all maximum frequency elements in matrix
class Frequency 
	def maxOccurrenceSum(matrix) 
		#  Get the length
		r = matrix.length
		c = matrix[0].length
		#  Use to count frequency
		record = Hash.new
		max = 1
		#  Execute loop through by matrix row
		i = 0
		while (i < r) 
			#  Execute loop through by matrix column
			j = 0
			while (j < c) 
				if (record.key?(matrix[i][j])) 
					record[matrix[i][j]] = record[matrix[i][j]] + 1
					if (max < record[matrix[i][j]]) 
						#  Get new max occurrence frequency
						max = record[matrix[i][j]]
					end
				else 
					record[matrix[i][j]] = 1
				end
				j += 1
			end

			i += 1
		end

		sum = 0
		record.each { | key, value | 
			if (value == max) 
				sum += key * value
			end
		}
		#  Display calculated result
		print(" Result ", sum)
	end

end

def main() 
	task = Frequency.new()
	matrix = [
		[1, 7, -2, 6, 5] , 
        [8, 3, 4, 5, 7] , 
        [6, 2, 1, 3, -2] , 
        [3, 2, 1, -2, 6]
	]
	#  Test
	#  (3+3+3) + (1+1+1) + (-2 + -2 + -2)
	#  (6+6+6)
	task.maxOccurrenceSum(matrix)
end

main()

Output

 Result 24
import scala.collection.mutable._;
// Scala Program
// Sum of all maximum frequency elements in matrix
class Frequency
{
	def maxOccurrenceSum(matrix: Array[Array[Int]]): Unit = {
		// Get the length
		var r: Int = matrix.length;
		var c: Int = matrix(0).length;
		// Use to count frequency
		var record= Map[Int, Int]();
		var max: Int = 1;
		// Execute loop through by matrix row
		var i: Int = 0;
		while (i < r)
		{
			// Execute loop through by matrix column
			var j: Int = 0;
			while (j < c)
			{
				if (record.contains(matrix(i)(j)))
				{
					// increase element frequency
					record.addOne(matrix(i)(j), record.get(matrix(i)(j)).get + 1);
					if (max < record.get(matrix(i)(j)).get)
					{
						// Get new max occurrence frequency
						max = record.get(matrix(i)(j)).get;
					}
				}
				else
				{
					// Add new element
					record.addOne(matrix(i)(j), 1);
				}
				j += 1;
			}
			i += 1;
		}
		var sum: Int = 0;
		for ((key, value) <- record)
		{
			if (value == max)
			{
				sum += key * value;
			}
		}
		// Display calculated result
		print(" Result " + sum);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Frequency = new Frequency();
		var matrix: Array[Array[Int]] = Array(Array(1, 7, -2, 6, 5), Array(8, 3, 4, 5, 7), Array(6, 2, 1, 3, -2), Array(3, 2, 1, -2, 6));
		// Test
		// (3+3+3) + (1+1+1) + (-2 + -2 + -2)
		// (6+6+6)
		task.maxOccurrenceSum(matrix);
	}
}

Output

 Result 24
import Foundation
// Swift 4 Program
// Sum of all maximum frequency elements in matrix
class Frequency
{
	func maxOccurrenceSum(_ matrix: [
		[Int]
	])
	{
		// Get the length
		let r: Int = matrix.count;
		let c: Int = matrix[0].count;
		// Use to count frequency
		var record = [Int: Int]();
		var max: Int = 1;
		// Execute loop through by matrix row
		var i: Int = 0;
		while (i < r)
		{
			// Execute loop through by matrix column
			var j: Int = 0;
			while (j < c)
			{
				if (record.keys.contains(matrix[i][j]))
				{
					// increase element frequency
					record[matrix[i][j]] = record[matrix[i][j]]! + 1;
					if (max < record[matrix[i][j]]!)
					{
						// Get new max occurrence frequency
						max = record[matrix[i][j]]!;
					}
				}
				else
				{
					// Add new element
					record[matrix[i][j]] = 1;
				}
				j += 1;
			}
			i += 1;
		}
		var sum: Int = 0;
		for (key, value) in record
		{
			if (value == max)
			{
				sum += key * value;
			}
		}
		// Display calculated result
		print(" Result ", sum, terminator: "");
	}
}
func main()
{
	let task: Frequency = Frequency();
	let matrix: [[Int]] = [
		[1, 7, -2, 6, 5] , 
        [8, 3, 4, 5, 7] , 
        [6, 2, 1, 3, -2] , 
        [3, 2, 1, -2, 6]
	];
	// Test
	// (3+3+3) + (1+1+1) + (-2 + -2 + -2)
	// (6+6+6)
	task.maxOccurrenceSum(matrix);
}
main();

Output

 Result  24
// Kotlin Program
// Sum of all maximum frequency elements in matrix
class Frequency
{
	fun maxOccurrenceSum(matrix: Array < Array < Int >> ): Unit
	{
		// Get the length
		var r: Int = matrix.count();
		var c: Int = matrix[0].count();
		// Use to count frequency
		var record = mutableMapOf < Int , Int > ();
		var max: Int = 1;
		// Execute loop through by matrix row
		var i: Int = 0;
		while (i < r)
		{
			// Execute loop through by matrix column
			var j: Int = 0;
			while (j < c)
			{
				if (record.containsKey(matrix[i][j]))
				{
					// increase element frequency
					record.put(matrix[i][j], record.getValue(matrix[i][j]) + 1);
					if (max < record.getValue(matrix[i][j]))
					{
						// Get new max occurrence frequency
						max = record.getValue(matrix[i][j]);
					}
				}
				else
				{
					// Add new element
					record.put(matrix[i][j], 1);
				}
				j += 1;
			}
			i += 1;
		}
		var sum: Int = 0;
		// Finds the sum of max occurring frequency
		for (key in record.keys)
		{
			if (record.getValue(key) == max)
			{
				sum += key * record.getValue(key);
			}
		}
		// Display calculated result
		print(" Result " + sum);
	}
}
fun main(args: Array < String > ): Unit
{
	var task: Frequency = Frequency();
	var matrix: Array < Array < Int >> = arrayOf(
      arrayOf(1, 7, -2, 6, 5), 
      arrayOf(8, 3, 4, 5, 7), 
      arrayOf(6, 2, 1, 3, -2), 
      arrayOf(3, 2, 1, -2, 6)
    );
	// Test
	// (3+3+3) + (1+1+1) + (-2 + -2 + -2)
	// (6+6+6)
	task.maxOccurrenceSum(matrix);
}

Output

 Result 24


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