{"id":21504,"date":"2021-09-22T17:36:53","date_gmt":"2021-09-22T12:06:53","guid":{"rendered":"https:\/\/python-programs.com\/?p=21504"},"modified":"2021-11-22T18:35:42","modified_gmt":"2021-11-22T13:05:42","slug":"python-program-to-find-sum-of-all-odd-frequency-elements-in-matrix","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-sum-of-all-odd-frequency-elements-in-matrix\/","title":{"rendered":"Python Program to Find Sum of all Odd Frequency Elements in Matrix"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Find Sum of all Even Frequency Elements in Matrix<\/a><\/p>\n Given a matrix, the task is to find the sum of all odd Frequency elements of the given Matrix in Python.<\/p>\n What is a matrix:<\/strong><\/p>\n 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.<\/p>\n Example:<\/strong><\/p>\n <\/p>\n Above is the matrix which contains 5 rows and 4 columns and having elements from 1 to 20.<\/p>\n In this order, the dimensions of a matrix indicate the number of rows and columns.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to find the sum of all the odd Frequency elements of the given Matrix in Python.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explore more Example Python Programs<\/a> with output and explanation and practice them for your interviews, assignments and stand out from the rest of the crowd.<\/p>\n In the previous article, we have discussed Python Program to Find Sum of all Even Frequency Elements in Matrix Given a matrix, the task is to find the sum of all odd Frequency elements of the given Matrix in Python. What is a matrix: A matrix is a rectangular sequence of numbers divided into columns …<\/p>\nGiven Matrix : \r\n1 2 2\r\n3 4 2\r\n5 1 5<\/pre>\n
The sum of all the odd frequency elements in the given matrix is = { 9 }<\/pre>\n
Here the odd Frequency elements in the given matrix are 2,4,3 so sum=2+4+3=9<\/pre>\n
Given Matrix : \r\n7 2\r\n2 1<\/pre>\n
The sum of all the odd frequency elements in the given matrix is = { 8 }<\/pre>\n
Program to Find Sum of all Odd Frequency Elements in Matrix<\/h2>\n
\n
Method #1: Using Counter() function (Hashing, Static Input)<\/h3>\n
\n
# import the Counter Function from collections using the import keyword\r\nfrom collections import Counter\r\n# Give the matrix as static input and store it in a variable.\r\nmtrx = [[1, 2, 2], [3, 4, 2], [5, 1, 5]]\r\n# Calculate the number of rows of the given matrix by\r\n# calculating the length of the nested list using the len() function\r\n# and store it in a variable mtrxrows.\r\nmtrxrows = len(mtrx)\r\n# Calculate the number of columns of the given matrix by\r\n# calculating the length of the first list in the nested list\r\n# using the len() function and store it in a variable mtrxcols.\r\nmtrxcols = len(mtrx[0])\r\n# take a list and initialize it to empty using [] or list() function\r\nmtrxlist = []\r\n# Loop till the given number of rows using the For loop.\r\nfor n in range(mtrxrows):\r\n # Inside the For loop, Iterate till the given number of columns using another\r\n # Nested For loop(Inner For loop).\r\n for m in range(mtrxcols):\r\n # add the gvnmatrix[n][m] value to the list using the append() function\r\n # where n is the iterator value of the parent For loop and m is the iterator\r\n # value of the inner For loop.\r\n mtrxlist.append(mtrx[n][m])\r\n# Apply the Counter function to the matrix elements list which stores the matrix element\r\n# as key and its frequency as value\r\nmtrxfreq = Counter(mtrxlist)\r\n# Take a variable(say oddFreqSum) that stores the sum of all the odd frequency elements\r\n# of the matrix and initialize its value to 0.\r\noddFreqSum = 0\r\n# Loop in this frequency dictionary using the For loop.\r\nfor key in mtrxfreq:\r\n # Check if any key has frequency odd using the if conditional statement.\r\n if(mtrxfreq[key] % 2 != 0):\r\n # If it is true then add the key(Matrix Element) to the oddFreqSum.\r\n oddFreqSum = oddFreqSum+key\r\n# Print the oddFreqSum.\r\nprint(\r\n 'The sum of all the odd frequency elements in the given matrix is = {', oddFreqSum, '}')\r\n<\/pre>\n
The sum of all the odd frequency elements in the given matrix is = { 9 }<\/pre>\n
Method #2: Using Counter() function (Hashing, User Input)<\/h3>\n
\n
# import the Counter Function from collections using the import keyword\r\nfrom collections import Counter\r\n# Give the number of rows of the matrix as user input using the int(input()) function\r\n# and store it in a variable.\r\nmtrxrows = int(input('Enter some random number of rows of the matrix = '))\r\n# Give the number of columns of the matrix as user input using the int(input()) function\r\n# and store it in another variable.\r\nmtrxcols = int(input('Enter some random number of columns of the matrix = '))\r\n# Take a list and initialize it with an empty value using [] or list() to say gvnmatrix.\r\nmtrx = []\r\n# Loop till the given number of rows using the For loop\r\nfor n in range(mtrxrows):\r\n # Inside the For loop, Give all the row elements of the given Matrix as a list using\r\n # the list(),map(),int(),split() functions and store it in a variable.\r\n l = list(map(int, input(\r\n 'Enter {'+str(mtrxcols)+'} elements of row {'+str(n+1)+'} separated by spaces = ').split()))\r\n # Add the above row elements list to gvnmatrix using the append() function.\r\n mtrx.append(l)\r\n\r\n# take a list and initialize it to empty using [] or list() function\r\nmtrxlist = []\r\n# Loop till the given number of rows using the For loop.\r\nfor n in range(mtrxrows):\r\n # Inside the For loop, Iterate till the given number of columns using another\r\n # Nested For loop(Inner For loop).\r\n for m in range(mtrxcols):\r\n # add the gvnmatrix[n][m] value to the list using the append() function\r\n # where n is the iterator value of the parent For loop and m is the iterator\r\n # value of the inner For loop.\r\n mtrxlist.append(mtrx[n][m])\r\n# Apply the Counter function to the matrix elements list which stores the matrix element\r\n# as key and its frequency as value\r\nmtrxfreq = Counter(mtrxlist)\r\n# Take a variable(say oddFreqSum) that stores the sum of all the odd frequency elements\r\n# of the matrix and initialize its value to 0.\r\noddFreqSum = 0\r\n# Loop in this frequency dictionary using the For loop.\r\nfor key in mtrxfreq:\r\n # Check if any key has frequency odd using the if conditional statement.\r\n if(mtrxfreq[key] % 2 != 0):\r\n # If it is true then add the key(Matrix Element) to the oddFreqSum.\r\n oddFreqSum = oddFreqSum+key\r\n# Print the oddFreqSum.\r\nprint(\r\n 'The sum of all the odd frequency elements in the given matrix is = {', oddFreqSum, '}')\r\n<\/pre>\n
Enter some random number of rows of the matrix = 2\r\nEnter some random number of columns of the matrix = 2\r\nEnter {2} elements of row {1} separated by spaces = 7 2\r\nEnter {2} elements of row {2} separated by spaces = 2 1\r\nThe sum of all the odd frequency elements in the given matrix is = { 8 }<\/pre>\n
\n