In the previous article, we have discussed Python Program to Calculate Sum of all Minimum Frequency Elements in Matrix

Given a matrix, the task is to find the sum of all Maximum occurring elements of the given Matrix in Python.

**What is a matrix:**

A matrix is a rectangular sequence of numbers divided into columns and rows. A matrix element or entry is a number that appears in a matrix.

**Example:**

Above is the matrix which contains 5 rows and 4 columns and having elements from 1 to 20.

In this order, the dimensions of a matrix indicate the number of rows and columns.

**Examples:**

**Example1:**

**Input:**

Given Matrix : 1 2 2 3 4 2 5 1 5

**Output:**

The sum of all the maximum frequency elements in the given matrix is = { 2 }

**Explanation:**

Here the element 2 have maximum frequency i.e 3 so sum=2

**Example2:**

**Input:**

Given Matrix : 5 5 5 7 7 7 1 2 3

**Output:**

The sum of all the maximum frequency elements in the given matrix is = { 12 }

## Program to Calculate Sum of all Maximum Occurring Elements in Matrix in Python

Below are the ways to find the sum of all maximum Occurring elements of the given Matrix in Python.

### Method #1: Using Counter() function (Hashing, Static Input)

**Approach:**

- Import the Counter Function from collections using the import keyword.
- Give the matrix as static input and store it in a variable.
- Calculate the number of rows of the given matrix by calculating the length of the nested list using the len() function and store it in a variable mtrxrows.
- Calculate the number of columns of the given matrix by calculating the length of the first list in the nested list using the len() function and store it in a variable mtrxcolums.
- Take a list and initialize it to empty using [] or list() function.
- Loop till the given number of rows using the For loop.
- Inside the For loop, Iterate till the given number of columns using another Nested For loop(Inner For loop).
- Add the gvnmatrix[n][m] value to the list using the append() function where n is the iterator value of the parent For loop and m is the iterator value of the inner For loop.
- Apply the Counter function to the matrix elements list which stores the matrix element as key and its frequency as value.
- Calculate the maximum frequency using the values() and max() function(Here values function convert the frequency dictionary values to list and we find the maximum value from it using the max() function) and store it in a variable say
**maxFreq.** - Take a variable(say maxFreqSum) that stores the sum of all the maximum frequency elements of the matrix and initialize its value to 0.
- Loop in this frequency dictionary using the For loop.
- Check if any key has a frequency equal to
**maxFreq**using the if conditional statement. - If it is true then add the key(Matrix Element) to the maxFreqSum
**.** - Print the maxFreqSum
**.** - The Exit of the Program.

**Below is the implementation:**

# import the Counter Function from collections using the import keyword from collections import Counter # Give the matrix as static input and store it in a variable. mtrx = [[1, 2, 2], [3, 4, 2], [5, 1, 5]] # Calculate the number of rows of the given matrix by # calculating the length of the nested list using the len() function # and store it in a variable mtrxrows. mtrxrows = len(mtrx) # Calculate the number of columns of the given matrix by # calculating the length of the first list in the nested list # using the len() function and store it in a variable mtrxcols. mtrxcols = len(mtrx[0]) # take a list and initialize it to empty using [] or list() function mtrxlist = [] # Loop till the given number of rows using the For loop. for n in range(mtrxrows): # Inside the For loop, Iterate till the given number of columns using another # Nested For loop(Inner For loop). for m in range(mtrxcols): # add the gvnmatrix[n][m] value to the list using the append() function # where n is the iterator value of the parent For loop and m is the iterator # value of the inner For loop. mtrxlist.append(mtrx[n][m]) # Apply the Counter function to the matrix elements list which stores the matrix element # as key and its frequency as value mtrxfreq = Counter(mtrxlist) # Calculate the maximum frequency using the values() and max() function # (Here values function convert the frequency dictionary values to list # and we find the maximum value from it using the max() function) # and store it in a variable say maxFreq. maxFreq = max(mtrxfreq.values()) # Take a variable(say maxFreqSum) that stores the sum of all the maximum frequency elements of the matrix # and initialize its value to 0. maxFreqSum = 0 # Loop in this frequency dictionary using the For loop. for key in mtrxfreq: # Check if any key has a frequency equal to maxFreq using the if conditional statement. if(mtrxfreq[key] == maxFreq): # If it is true then add the key(Matrix Element) to the maxFreqSum. maxFreqSum = maxFreqSum+key # Print the maxFreqSum. print( 'The sum of all the maximum frequency elements in the given matrix is = {', maxFreqSum, '}')

**Output:**

The sum of all the maximum frequency elements in the given matrix is = { 2 }

### Method #2: Using Counter() function (Hashing, User Input)

**Approach:**

- Import the Counter Function from collections using the import keyword.
- Give the number of rows of the matrix as user input using the int(input()) function and store it in a variable.
- Give the number of columns of the matrix as user input using the int(input()) function and store it in another variable.
- Take a list and initialize it with an empty value using [] or list() to say
**gvnmatrix**. - Loop till the given number of rows using the For loop
- Inside the For loop, Give all the row elements of the given Matrix as a list using the list(),map(),int(),split() functions and store it in a variable.
- Add the above row elements list to
**gvnmatrix**using the append() function. - Take a list and initialize it to empty using [] or list() function.
- Loop till the given number of rows using the For loop.
- Inside the For loop, Iterate till the given number of columns using another Nested For loop(Inner For loop).
- Add the gvnmatrix[n][m] value to the list using the append() function where n is the iterator value of the parent For loop and m is the iterator value of the inner For loop.
- Apply the Counter function to the matrix elements list which stores the matrix element as key and its frequency as value
- Calculate the maximum frequency using the values() and max() function(Here values function convert the frequency dictionary values to list and we find the maximum value from it using the max() function) and store it in a variable say
**maxFreq.** - Take a variable(say maxFreqSum) that stores the sum of all the maximum frequency elements of the matrix and initialize its value to 0.
- Loop in this frequency dictionary using the For loop.
- Check if any key has a frequency equal to
**maxFreq**using the if conditional statement. - If it is true then add the key(Matrix Element) to the maxFreqSum
**.** - Print the maxFreqSum
**.** - The Exit of the Program.

**Below is the implementation:**

# import the Counter Function from collections using the import keyword from collections import Counter # Give the number of rows of the matrix as user input using the int(input()) function # and store it in a variable. mtrxrows = int(input('Enter some random number of rows of the matrix = ')) # Give the number of columns of the matrix as user input using the int(input()) function # and store it in another variable. mtrxcols = int(input('Enter some random number of columns of the matrix = ')) # Take a list and initialize it with an empty value using [] or list() to say gvnmatrix. mtrx = [] # Loop till the given number of rows using the For loop for n in range(mtrxrows): # Inside the For loop, Give all the row elements of the given Matrix as a list using # the list(),map(),int(),split() functions and store it in a variable. l = list(map(int, input( 'Enter {'+str(mtrxcols)+'} elements of row {'+str(n+1)+'} separated by spaces = ').split())) # Add the above row elements list to gvnmatrix using the append() function. mtrx.append(l) # take a list and initialize it to empty using [] or list() function mtrxlist = [] # Loop till the given number of rows using the For loop. for n in range(mtrxrows): # Inside the For loop, Iterate till the given number of columns using another # Nested For loop(Inner For loop). for m in range(mtrxcols): # add the gvnmatrix[n][m] value to the list using the append() function # where n is the iterator value of the parent For loop and m is the iterator # value of the inner For loop. mtrxlist.append(mtrx[n][m]) # Apply the Counter function to the matrix elements list which stores the matrix element # as key and its frequency as value mtrxfreq = Counter(mtrxlist) # Calculate the maximum frequency using the values() and max() function # (Here values function convert the frequency dictionary values to list # and we find the maximum value from it using the max() function) # and store it in a variable say maxFreq. maxFreq = max(mtrxfreq.values()) # Take a variable(say maxFreqSum) that stores the sum of all the maximum frequency elements of the matrix # and initialize its value to 0. maxFreqSum = 0 # Loop in this frequency dictionary using the For loop. for key in mtrxfreq: # Check if any key has a frequency equal to maxFreq using the if conditional statement. if(mtrxfreq[key] == maxFreq): # If it is true then add the key(Matrix Element) to the maxFreqSum. maxFreqSum = maxFreqSum+key # Print the maxFreqSum. print( 'The sum of all the maximum frequency elements in the given matrix is = {', maxFreqSum, '}')

**Output:**

Enter some random number of rows of the matrix = 3 Enter some random number of columns of the matrix = 3 Enter {3} elements of row {1} separated by spaces = 5 5 5 Enter {3} elements of row {2} separated by spaces = 7 7 7 Enter {3} elements of row {3} separated by spaces = 1 2 3 The sum of all the maximum frequency elements in the given matrix is = { 12 }

If you are new to the Python Programming Language then practice using our Python Programming Examples for Beginners as our expert team has designed them from scratch.