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, 4, 9, 8, 5, 1, 1]

**Output:**

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 : 11

**Example2:**

**Input:**

Given List = [10, 20, 30, 40, 20, 25]

**Output:**

The sum of elements in the given list [10, 20, 30, 40, 20, 25] which are having maximum frequency are : 20

## Program to Find the sum of all Highest Occurring Elements in an Array/List in Python

Below are the ways to find the sum of the maximum frequency elements of a given list in python:

- Using Dictionary (Hashing, User Input)
- Using Counter() function (Hashing, Static Input)
- Using Counter() function (Hashing, User Input)
- Using Counter() function (Hashing, User Input)

### Method #1: Using Dictionary (Hashing, Static Input)

**Approach:**

- Take a dictionary and initialize it to empty using the {} or dict() say
**freqncyDictionary**. - Give the list as static input and store it in a variable.
- Loop in the given list using the For loop.
- Inside the For loop, Check if the list element is present in the dictionary or not using the if conditional statement and ‘
**in**‘ keyword. - If it is true then increment the count of the list element in the dictionary by 1.
- Else initialize the dictionary with the list element as key and value as 1.
- Take a variable that stores the sum of all the most occurring elements in the given list and initialize its value to 0.
- Find the maximum frequency in the freqncyDictionary
- We can find it by converting freqncyDictionary values to list(using list and values() functions) and using max() function.
- Loop in the freqncyDictionary using the For loop.
- Check if any element value having a frequency equal to max frequency using the if conditional statement.
- If it is true then increment the count by the key(list element).
- The Exit of the program.

**Below is the implementation:**

# Take a dictionary and initialize it to empty # using the {} or dict() say freqncyDictionary. freqncyDictionary = {} # Give the list as static input and store it in a variable. gvnlst = [4, 5, 5, 2, 4, 2, 2, 3, 3, 4, 9, 8, 5, 1, 1] # Loop in the given list using the For loop. for i in gvnlst: # Inside the For loop, # Check if the list element is present in the dictionary # or not using the if conditional statement and 'in' keyword. if i in freqncyDictionary.keys(): # If it is true then increment the count of the list element # in the dictionary by 1. freqncyDictionary[i] = freqncyDictionary[i]+1 # Else initialize the dictionary with the list element as key and value as 1. else: freqncyDictionary[i] = 1 # Take a variable which stores sum of all the most occuring elements in the given list # and initialize its value to 0 summaxfreqelemnts = 0 # find the maximum frequency in the freqncyDictionary # We can find it by converting freqncyDictionary values to list(using list and values() functions) # and using max() function maxfrqncy = max(freqncyDictionary.values()) print('The sum of elements in the given list', gvnlst, 'which are having maximum frequency are :') # loop in the freqncyDictionary using the For loop for key in freqncyDictionary: # check if any element value having frequency equal to max frequency using the if conditional statement. if(freqncyDictionary[key] == maxfrqncy): # if it is true then increment the count by the key(list element) summaxfreqelemnts = summaxfreqelemnts+key print(summaxfreqelemnts)

**Output:**

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 : 11

### Method #2: Using Dictionary (Hashing, User Input)

**Approach:**

- Take a dictionary and initialize it to empty using the {} or dict() say
**freqncyDictionary**. - Give the list as user input using the list(),map(),split(),int functions and store it in a variable.
- Loop in the given list using the For loop.
- Inside the For loop, Check if the list element is present in the dictionary or not using the if conditional statement and ‘
**in**‘ keyword. - If it is true then increment the count of the list element in the dictionary by 1.
- Else initialize the dictionary with the list element as key and value as 1.
- Take a variable that stores the sum of all the most occurring elements in the given list and initialize its value to 0.
- Find the maximum frequency in the freqncyDictionary
- We can find it by converting freqncyDictionary values to list(using list and values() functions) and using max() function.
- Loop in the freqncyDictionary using the For loop.
- Check if any element value having a frequency equal to max frequency using the if conditional statement.
- If it is true then increment the count by the key(list element).
- The Exit of the program.

**Below is the implementation:**

# Take a dictionary and initialize it to empty # using the {} or dict() say freqncyDictionary. freqncyDictionary = {} # Give the list as user input using the list(),map(),split(),int functions and # store it in a variable. gvnlst = list( map(int, input('Enter some random list element separated by spaces = ').split())) # Loop in the given list using the For loop. for i in gvnlst: # Inside the For loop, # Check if the list element is present in the dictionary # or not using the if conditional statement and 'in' keyword. if i in freqncyDictionary.keys(): # If it is true then increment the count of the list element # in the dictionary by 1. freqncyDictionary[i] = freqncyDictionary[i]+1 # Else initialize the dictionary with the list element as key and value as 1. else: freqncyDictionary[i] = 1 # Take a variable which stores sum of all the most occuring elements in the given list # and initialize its value to 0 summaxfreqelemnts = 0 # find the maximum frequency in the freqncyDictionary # We can find it by converting freqncyDictionary values to list(using list and values() functions) # and using max() function maxfrqncy = max(freqncyDictionary.values()) print('The sum of elements in the given list', gvnlst, 'which are having maximum frequency are :') # loop in the freqncyDictionary using the For loop for key in freqncyDictionary: # check if any element value having frequency equal to max frequency using the if conditional statement. if(freqncyDictionary[key] == maxfrqncy): # if it is true then increment the count by the key(list element) summaxfreqelemnts = summaxfreqelemnts+key print(summaxfreqelemnts)

**Output:**

Enter some random list element separated by spaces = 1 2 3 4 1 2 6 84 6 The sum of elements in the given list [1, 2, 3, 4, 1, 2, 6, 84, 6] which are having maximum frequency are : 9

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

**Approach:**

- Import the Counter() function from collections using the import keyword.
- Give the list as static input and store it in a variable.
- Calculate the frequency of all the given list elements using the Counter() function which returns the element and its frequency as key-value pair and store this dictionary in a variable(say
**freqncyDictionary**) - Take a variable that stores the sum of all the most occurring elements in the given list and initialize its value to 0.
- Find the maximum frequency in the freqncyDictionary
- We can find it by converting freqncyDictionary values to list(using list and values() functions) and using max() function.
- Loop in the freqncyDictionary using the For loop.
- Check if any element value having a frequency equal to max frequency using the if conditional statement.
- If it is true then increment the count by the key(list element).
- 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 list as static input and store it in a variable. gvnlst = [4, 5, 5, 2, 4, 2, 2, 3, 3, 4, 9, 8, 5, 1, 1] # Calculate the frequency of all the given list elements using the Counter() # function which returns the element and its frequency as key-value pair # and store this dictionary in a variable(say freqncyDictionary) freqncyDictionary = Counter(gvnlst) # Take a variable which stores sum of all the most occuring elements in the given list # and initialize its value to 0 summaxfreqelemnts = 0 # find the maximum frequency in the freqncyDictionary # We can find it by converting freqncyDictionary values to list(using list and values() functions) # and using max() function maxfrqncy = max(freqncyDictionary.values()) print('The sum of elements in the given list', gvnlst, 'which are having maximum frequency are :') # loop in the freqncyDictionary using the For loop for key in freqncyDictionary: # check if any element value having frequency equal to max frequency using the if conditional statement. if(freqncyDictionary[key] == maxfrqncy): # if it is true then increment the count by the key(list element) summaxfreqelemnts = summaxfreqelemnts+key print(summaxfreqelemnts)

**Output:**

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 : 11

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

**Approach:**

- Import the Counter() function from collections using the import keyword.
- Give the list as user input using the list(),map(),split(),int functions and store it in a variable.
- Calculate the frequency of all the given list elements using the Counter() function which returns the element and its frequency as key-value pair and store this dictionary in a variable(say
**freqncyDictionary**) - Find the maximum frequency in the freqncyDictionary
- Loop in the freqncyDictionary using the For loop.
- If it is true then increment the count by the key(list element).
- 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 list as user input using the list(),map(),split(),int functions # and store it in a variable. gvnlst = list( map(int, input('Enter some random list element separated by spaces = ').split())) # Calculate the frequency of all the given list elements using the Counter() # function which returns the element and its frequency as key-value pair # and store this dictionary in a variable(say freqncyDictionary) freqncyDictionary = Counter(gvnlst) # Take a variable which stores sum of all the most occuring elements in the given list # and initialize its value to 0 summaxfreqelemnts = 0 # find the maximum frequency in the freqncyDictionary # We can find it by converting freqncyDictionary values to list(using list and values() functions) # and using max() function maxfrqncy = max(freqncyDictionary.values()) print('The sum of elements in the given list', gvnlst, 'which are having maximum frequency are :') # loop in the freqncyDictionary using the For loop for key in freqncyDictionary: # check if any element value having frequency equal to max frequency using the if conditional statement. if(freqncyDictionary[key] == maxfrqncy): # if it is true then increment the count by the key(list element) summaxfreqelemnts = summaxfreqelemnts+key print(summaxfreqelemnts)

**Output:**

Enter some random list element separated by spaces = 10 20 30 40 20 25 The sum of elements in the given list [10, 20, 30, 40, 20, 25] which are having maximum frequency are : 20

Grab the opportunity and utilize the Python Program Code Examples over here to prepare basic and advanced topics too with ease and clear all your doubts.