{"id":20634,"date":"2021-09-21T08:59:11","date_gmt":"2021-09-21T03:29:11","guid":{"rendered":"https:\/\/python-programs.com\/?p=20634"},"modified":"2021-11-22T18:36:18","modified_gmt":"2021-11-22T13:06:18","slug":"python-program-to-find-the-sum-of-all-highest-occurring-elements-in-an-array-list","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-the-sum-of-all-highest-occurring-elements-in-an-array-list\/","title":{"rendered":"Python Program to Find the sum of all Highest Occurring Elements in an Array\/List"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Find the Sum of all Odd Frequency Elements in an Array\/List<\/a><\/p>\n Given a list, the task is to find the sum of the maximum frequency elements of a given list.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to find the sum of the maximum frequency elements of a given list 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 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 Grab the opportunity and utilize the Python Program Code Examples<\/a> over here to prepare basic and advanced topics too with ease and clear all your doubts.<\/p>\n In the previous article, we have discussed Python Program to Find the Sum of all Odd Frequency Elements in an Array\/List Given a list, the task is to find the sum of the maximum frequency elements of a given list. Examples: Example1: Input: Given List = [4, 5, 5, 2, 4, 2, 2, 3, 3, …<\/p>\nGiven List = [4, 5, 5, 2, 4, 2, 2, 3, 3, 4, 9, 8, 5, 1, 1]<\/pre>\n
The sum of elements in the given list [4, 5, 5, 2, 4, 2, 2, 3, 3, 4, 9, 8, 5, 1, 1] which are having maximum frequency are :\r\n11<\/pre>\n
Given List = [10, 20, 30, 40, 20, 25]<\/pre>\n
The sum of elements in the given list [10, 20, 30, 40, 20, 25] which are having maximum frequency are :\r\n20<\/pre>\n
Program to Find the sum of all Highest Occurring Elements in an Array\/List in Python<\/h2>\n
\n
Method #1: Using Dictionary (Hashing, Static Input)<\/h3>\n
\n
# Take a dictionary and initialize it to empty\r\n# using the {} or dict() say freqncyDictionary.\r\nfreqncyDictionary = {}\r\n# Give the list as static input and store it in a variable.\r\ngvnlst = [4, 5, 5, 2, 4, 2, 2, 3, 3, 4, 9, 8, 5, 1, 1]\r\n# Loop in the given list using the For loop.\r\nfor i in gvnlst:\r\n # Inside the For loop,\r\n # Check if the list element is present in the dictionary\r\n # or not using the if conditional statement and 'in' keyword.\r\n if i in freqncyDictionary.keys():\r\n # If it is true then increment the count of the list element\r\n # in the dictionary by 1.\r\n freqncyDictionary[i] = freqncyDictionary[i]+1\r\n # Else initialize the dictionary with the list element as key and value as 1.\r\n else:\r\n freqncyDictionary[i] = 1\r\n# Take a variable which stores sum of all the most occuring elements in the given list\r\n# and initialize its value to 0\r\nsummaxfreqelemnts = 0\r\n# find the maximum frequency in the freqncyDictionary\r\n# We can find it by converting freqncyDictionary values to list(using list and values() functions)\r\n# and using max() function\r\nmaxfrqncy = max(freqncyDictionary.values())\r\nprint('The sum of elements in the given list', gvnlst,\r\n 'which are having maximum frequency are :')\r\n# loop in the freqncyDictionary using the For loop\r\nfor key in freqncyDictionary:\r\n # check if any element value having frequency equal to max frequency using the if conditional statement.\r\n if(freqncyDictionary[key] == maxfrqncy):\r\n # if it is true then increment the count by the key(list element)\r\n summaxfreqelemnts = summaxfreqelemnts+key\r\nprint(summaxfreqelemnts)\r\n<\/pre>\n
The sum of elements in the given list [4, 5, 5, 2, 4, 2, 2, 3, 3, 4, 9, 8, 5, 1, 1] which are having maximum frequency are :\r\n11<\/pre>\n
Method #2: Using Dictionary (Hashing, User Input)<\/h3>\n
\n
# Take a dictionary and initialize it to empty\r\n# using the {} or dict() say freqncyDictionary.\r\nfreqncyDictionary = {}\r\n# Give the list as user input using the list(),map(),split(),int functions and \r\n# store it in a variable.\r\ngvnlst = list( map(int, input('Enter some random list element separated by spaces = ').split()))\r\n# Loop in the given list using the For loop.\r\nfor i in gvnlst:\r\n # Inside the For loop,\r\n # Check if the list element is present in the dictionary\r\n # or not using the if conditional statement and 'in' keyword.\r\n if i in freqncyDictionary.keys():\r\n # If it is true then increment the count of the list element\r\n # in the dictionary by 1.\r\n freqncyDictionary[i] = freqncyDictionary[i]+1\r\n # Else initialize the dictionary with the list element as key and value as 1.\r\n else:\r\n freqncyDictionary[i] = 1\r\n# Take a variable which stores sum of all the most occuring elements in the given list\r\n# and initialize its value to 0\r\nsummaxfreqelemnts = 0\r\n# find the maximum frequency in the freqncyDictionary\r\n# We can find it by converting freqncyDictionary values to list(using list and values() functions)\r\n# and using max() function\r\nmaxfrqncy = max(freqncyDictionary.values())\r\nprint('The sum of elements in the given list', gvnlst,\r\n 'which are having maximum frequency are :')\r\n# loop in the freqncyDictionary using the For loop\r\nfor key in freqncyDictionary:\r\n # check if any element value having frequency equal to max frequency using the if conditional statement.\r\n if(freqncyDictionary[key] == maxfrqncy):\r\n # if it is true then increment the count by the key(list element)\r\n summaxfreqelemnts = summaxfreqelemnts+key\r\nprint(summaxfreqelemnts)\r\n<\/pre>\n
Enter some random list element separated by spaces = 1 2 3 4 1 2 6 84 6\r\nThe sum of elements in the given list [1, 2, 3, 4, 1, 2, 6, 84, 6] which are having maximum frequency are :\r\n9<\/pre>\n
Method #3: 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 list as static input and store it in a variable.\r\ngvnlst = [4, 5, 5, 2, 4, 2, 2, 3, 3, 4, 9, 8, 5, 1, 1]\r\n# Calculate the frequency of all the given list elements using the Counter()\r\n# function which returns the element and its frequency as key-value pair\r\n# and store this dictionary in a variable(say freqncyDictionary)\r\nfreqncyDictionary = Counter(gvnlst)\r\n# Take a variable which stores sum of all the most occuring elements in the given list\r\n# and initialize its value to 0\r\nsummaxfreqelemnts = 0\r\n# find the maximum frequency in the freqncyDictionary\r\n# We can find it by converting freqncyDictionary values to list(using list and values() functions)\r\n# and using max() function\r\nmaxfrqncy = max(freqncyDictionary.values())\r\nprint('The sum of elements in the given list', gvnlst,\r\n 'which are having maximum frequency are :')\r\n# loop in the freqncyDictionary using the For loop\r\nfor key in freqncyDictionary:\r\n # check if any element value having frequency equal to max frequency using the if conditional statement.\r\n if(freqncyDictionary[key] == maxfrqncy):\r\n # if it is true then increment the count by the key(list element)\r\n summaxfreqelemnts = summaxfreqelemnts+key\r\nprint(summaxfreqelemnts)\r\n<\/pre>\n
The sum of elements in the given list [4, 5, 5, 2, 4, 2, 2, 3, 3, 4, 9, 8, 5, 1, 1] which are having maximum frequency are :\r\n11<\/pre>\n
Method #4: 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 list as user input using the list(),map(),split(),int functions\r\n# and store it in a variable.\r\ngvnlst = list(\r\n map(int, input('Enter some random list element separated by spaces = ').split()))\r\n# Calculate the frequency of all the given list elements using the Counter()\r\n# function which returns the element and its frequency as key-value pair\r\n# and store this dictionary in a variable(say freqncyDictionary)\r\nfreqncyDictionary = Counter(gvnlst)\r\n# Take a variable which stores sum of all the most occuring elements in the given list\r\n# and initialize its value to 0\r\nsummaxfreqelemnts = 0\r\n# find the maximum frequency in the freqncyDictionary\r\n# We can find it by converting freqncyDictionary values to list(using list and values() functions)\r\n# and using max() function\r\nmaxfrqncy = max(freqncyDictionary.values())\r\nprint('The sum of elements in the given list', gvnlst,\r\n 'which are having maximum frequency are :')\r\n# loop in the freqncyDictionary using the For loop\r\nfor key in freqncyDictionary:\r\n # check if any element value having frequency equal to max frequency using the if conditional statement.\r\n if(freqncyDictionary[key] == maxfrqncy):\r\n # if it is true then increment the count by the key(list element)\r\n summaxfreqelemnts = summaxfreqelemnts+key\r\nprint(summaxfreqelemnts)<\/pre>\n
Enter some random list element separated by spaces = 10 20 30 40 20 25\r\nThe sum of elements in the given list [10, 20, 30, 40, 20, 25] which are having maximum frequency are :\r\n20<\/pre>\n
\n