Sum of all even frequency elements in matrix

Here given code implementation process.

// Java Program 
// Sum of all even frequency elements in matrix
import java.util.HashMap;
public class Frequency
{
    
    public void evenFrequencySum(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 > ();
        // 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);
                }
                else
                {
                    // Add new element
                    record.put(matrix[i][j], 1);
                }  
            }
        }

        int sum = 0;

        for (int key: record.keySet())
        {
            
            if(record.get(key) % 2 == 0)
            {
                // Sum all even occurring elements 
                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,  4,  6,  5},
            {8, 9,  4,  5, 1},
            {6, 2, 1,  9,  -2},
            {9, 2, 1,  -2,  6},
        };
        // Even Frequency 
        // (1 + 1 + 1 + 1) + (2 + 2) + (-2 + -2)
        // ( 5 + 5) + ( 4 + 4 )
        task.evenFrequencySum(matrix);

    }
}

Output

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

// C++ Program
// Sum of all even frequency elements in matrix
class Frequency
{
	public: void evenFrequencySum(int matrix[R][C])
	{
		// Use to count frequency
		unordered_map < int, int > record ;
		// 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;
				}
				else
				{
					// Add new element
					record[matrix[i][j]] = 1;
				}
			}
		}
		int sum = 0;
		for (auto &data: record)
		{
			if ((data.second % 2) == 0)
			{
				// Sum all even occurring elements
				sum += data.second * data.first;
			}
		}
		cout << " Result " << sum;
	}
};
int main()
{
	Frequency task = Frequency();
	int matrix[R][C] = {
		{
			1 , 7 , 4 , 6 , 5
		} , {
			8 , 9 , 4 , 5 , 1
		} , {
			6 , 2 , 1 , 9 , -2
		} , {
			9 , 2 , 1 , -2 , 6
		} 
	};
	// Even Frequency
	// (1 + 1 + 1 + 1) + (2 + 2) + (-2 + -2)
	// ( 5 + 5) + ( 4 + 4 )
	task.evenFrequencySum(matrix);
	return 0;
}

Output

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

// C# Program
// Sum of all even frequency elements in matrix

public class Frequency
{
	public void evenFrequencySum(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 > ();
		// 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;
				}
				else
				{
					// Add new element
					record.Add(matrix[i,j], 1);
				}
			}
		}
		int sum = 0;
		foreach(KeyValuePair < int, int > data in record)
		{
			if (data.Value % 2 == 0)
			{
				// Sum all even occurring elements
				sum += data.Key * data.Value;
			}
		}
		// Display calculated result
		Console.Write(" Result " + sum);
	}
	public static void Main(String[] args)
	{
		Frequency task = new Frequency();
		int[,] matrix = {
			{
				1 , 7 , 4 , 6 , 5
			} , {
				8 , 9 , 4 , 5 , 1
			} , {
				6 , 2 , 1 , 9 , -2
			} , {
				9 , 2 , 1 , -2 , 6
			} 
    	};
		// Even Frequency
		// (1 + 1 + 1 + 1) + (2 + 2) + (-2 + -2)
		// ( 5 + 5) + ( 4 + 4 )
		task.evenFrequencySum(matrix);
	}
}

Output

 Result 22
<?php
// Php Program
// Sum of all even frequency elements in matrix
class Frequency
{
	public	function evenFrequencySum( & $matrix)
	{
		// Get the length
		$r = count($matrix);
		$c = count($matrix[0]);
		// Use to count frequency
		$record = array();
		// 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;
				}
				else
				{ // Add new element
					$record[$matrix[$i][$j]] = 1;
				}
			}
		}
		$sum = 0;
		foreach($record as $key => $value)
		{
			if (($value % 2) == 0)
			{
				// Sum all even occurring elements
				$sum += $key * $record[$key];
			}
		}
		// Display calculated result
		echo " Result ".$sum;
	}
}

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

Output

 Result 22
// Node Js Program
// Sum of all even frequency elements in matrix
class Frequency
{
	evenFrequencySum(matrix)
	{
		// Get the length
		var r = matrix.length;
		var c = matrix[0].length;
		// Use to count frequency
		var record = new Map();
		// 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);
				}
				else
				{
					// Add new element
					record.set(matrix[i][j], 1);
				}
			}
		}
		var sum = 0;
		for (let [key, value] of record)
		{
			if ((value % 2) == 0)
			{
				// Sum all even occurring elements
				sum += key * value;
			}
		}
		// Display calculated result
		process.stdout.write(" Result " + sum);
	}
}

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

Output

 Result 22
#  Python 3 Program
#  Sum of all even frequency elements in matrix
class Frequency :
	def evenFrequencySum(self, matrix) :
		#  Get the length
		r = len(matrix)
		c = len(matrix[0])
		#  Use to count frequency
		record = dict()
		#  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
				else :
					#  Add new element
					record[matrix[i][j]] = 1
				
				j += 1
			
			i += 1
		
		sum = 0
		for key, value in record.items() :
			if ((value % 2) == 0) :
				#  Sum all even occurring elements
				sum += key * value
			
		
		#  Display calculated result
		print(" Result ", sum, end = "")
	

def main() :
	task = Frequency()
	matrix = [
		[1, 7, 4, 6, 5] , 
        [8, 9, 4, 5, 1] , 
        [6, 2, 1, 9, -2] , 
        [9, 2, 1, -2, 6]
	] #  Even Frequency
	#  (1 + 1 + 1 + 1) + (2 + 2) + (-2 + -2)
	#  ( 5 + 5) + ( 4 + 4 )
	task.evenFrequencySum(matrix)

if __name__ == "__main__": main()

Output

 Result  22
#  Ruby Program
#  Sum of all even frequency elements in matrix
class Frequency 
	def evenFrequencySum(matrix) 
		#  Get the length
		r = matrix.length
		c = matrix[0].length
		#  Use to count frequency
		record = Hash.new
		#  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
				else 
					record[matrix[i][j]] = 1
				end

				j += 1
			end

			i += 1
		end

		sum = 0
		record.each { | key, value | 
			if ((value % 2) == 0) 
				#  Sum all even occurring elements
				sum += key * value
			end
		}
		#  Display calculated result
		print(" Result ", sum)
	end

end

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

main()

Output

 Result 22
import scala.collection.mutable._;
// Scala Program
// Sum of all even frequency elements in matrix
class Frequency
{
	def evenFrequencySum(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]();
		// 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);
				}
				else
				{
					// Add new element
					record.addOne(matrix(i)(j), 1);
				}
				j += 1;
			}
			i += 1;
		}
		var sum: Int = 0;
		for ((key, value) <- record)
		{
			if ((value % 2) == 0)
			{
				// Sum all even occurring elements
				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, 4, 6, 5), 
                Array(8, 9, 4, 5, 1), 
                Array(6, 2, 1, 9, -2), 
                Array(9, 2, 1, -2, 6)
         );
		// Even Frequency
		// (1 + 1 + 1 + 1) + (2 + 2) + (-2 + -2)
		// ( 5 + 5) + ( 4 + 4 )
		task.evenFrequencySum(matrix);
	}
}

Output

 Result 22
import Foundation
// Swift 4 Program
// Sum of all even frequency elements in matrix
class Frequency
{
	func evenFrequencySum(_ matrix: [[Int]])
	{
		// Get the length
		let r: Int = matrix.count;
		let c: Int = matrix[0].count;
		// Use to count frequency
		var record = [Int: Int]();
		// 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;
				}
				else
				{
					// Add new element
					record[matrix[i][j]] = 1;
				}
				j += 1;
			}
			i += 1;
		}
		var sum: Int = 0;
		for (key, value) in record
		{
			if ((value % 2) == 0)
			{
				// Sum all even occurring elements
				sum += key * value;
			}
		}
		// Display calculated result
		print(" Result ", sum, terminator: "");
	}
}
func main()
{
	let task: Frequency = Frequency();
	let matrix: [[Int]] = [
		[1, 7, 4, 6, 5] , 
        [8, 9, 4, 5, 1] , 
        [6, 2, 1, 9, -2] , 
        [9, 2, 1, -2, 6]
	];
	// Even Frequency
	// (1 + 1 + 1 + 1) + (2 + 2) + (-2 + -2)
	// ( 5 + 5) + ( 4 + 4 )
	task.evenFrequencySum(matrix);
}
main();

Output

 Result  22
// Kotlin Program
// Sum of all even frequency elements in matrix
class Frequency
{
	fun evenFrequencySum(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 > ();
		// 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);
				}
				else
				{
					// Add new element
					record.put(matrix[i][j], 1);
				}
				j += 1;
			}
			i += 1;
		}
		var sum: Int = 0;
		for (key in record.keys)
		{
			if ((record.getValue(key) % 2) == 0)
			{
				// Sum all even occurring elements
				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, 4, 6, 5), 
        arrayOf(8, 9, 4, 5, 1), 
        arrayOf(6, 2, 1, 9, -2), 
        arrayOf(9, 2, 1, -2, 6)
    );
	// Even Frequency
	// (1 + 1 + 1 + 1) + (2 + 2) + (-2 + -2)
	// ( 5 + 5) + ( 4 + 4 )
	task.evenFrequencySum(matrix);
}

Output

 Result 22


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