Python Program to Find the Sum of all Even Occurring Elements in an Array/List

In the previous article, we have discussed Python Program to Find the sum of all Highest Occurring Elements in an Array/List

Given a list, the task is to find the sum of the elements which are having an even frequency in the given array/List.

Examples:

Example1:

Input:

Given List = [6, 1, 4, 1, 1, 6, 4, 4, 2, 2]

Output:

The sum of all even frequency elements in the given list [6, 1, 4, 1, 1, 6, 4, 4, 2, 2] is:
8

Explanation:

Here 6,2 are the elements in the given list which are having even frequency
sum=6+2=8

Example2:

Input:

Given List = [7, 6, 8, 8, 1, 4, 1, 5, 4, 7]

Output:

The sum of all even frequency elements in the given list [7, 6, 8, 8, 1, 4, 1, 5, 4, 7] is:
20

Program to Find the Sum of all Even Occurring Elements in an Array/List in Python

Below are the ways to find the sum of the elements which are having an even frequency in the given array/List:

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 say evenfreqncycnt and initialize its value to 0.
  • Loop in the freqncyDictionary using the For loop.
  • Check if the key in the freqncyDictionary having value even using the if conditional statement.
  • If it is true then increment the evenfreqncycnt by the key and store it in the same variable.
  • After the end of For loop then print the evenfreqncycnt value.
  • 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 = [6, 1, 4, 1, 1, 6, 4, 4, 2, 2]
# 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 say evenfreqncycnt and initialize its value to 0.
evenfreqncycnt = 0
# Loop in the freqncyDictionary using the For loop.
for elemnt in freqncyDictionary:
    # Check if the key in the freqncyDictionary having value even
    # using the if conditional statement.
    if(freqncyDictionary[elemnt] % 2 == 0):
                # If it is true then increment the evenfreqncycnt by the key
        # and store it in the same variable.
        evenfreqncycnt += elemnt
# After the end of For loop then print the evenfreqncycnt value.
print('The sum of all even frequency elements in the given list', gvnlst, 'is:')
print(evenfreqncycnt)

Output:

The sum of all even frequency elements in the given list [6, 1, 4, 1, 1, 6, 4, 4, 2, 2] is:
8

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 say evenfreqncycnt and initialize its value to 0.
  • Loop in the freqncyDictionary using the For loop.
  • Check if the key in the freqncyDictionary having value even using the if conditional statement.
  • If it is true then increment the evenfreqncycnt by the key and store it in the same variable.
  • After the end of For loop then print the evenfreqncycnt value.
  • 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 say evenfreqncycnt and initialize its value to 0.
evenfreqncycnt = 0
# Loop in the freqncyDictionary using the For loop.
for elemnt in freqncyDictionary:
    # Check if the key in the freqncyDictionary having value even
    # using the if conditional statement.
    if(freqncyDictionary[elemnt] % 2 == 0):
                # If it is true then increment the evenfreqncycnt by the key
        # and store it in the same variable.
        evenfreqncycnt += elemnt
# After the end of For loop then print the evenfreqncycnt value.
print('The sum of all even frequency elements in the given list', gvnlst, 'is:')
print(evenfreqncycnt)

Output:

Enter some random list element separated by spaces = 1 2 3 4 5 1 3 4
The sum of all even frequency elements in the given list [1, 2, 3, 4, 5, 1, 3, 4] is:
8

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 say evenfreqncycnt and initialize its value to 0.
  • Loop in the freqncyDictionary using the For loop.
  • Check if the key in the freqncyDictionary having value even using the if conditional statement.
  • If it is true then increment the evenfreqncycnt by the key and store it in the same variable.
  • After the end of For loop then print the evenfreqncycnt value.
  • 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 = [10, 4, 2, 10, 1, 2, 3, 4, 5, 6, 6]
# 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 say evenfreqncycnt and initialize its value to 0.
evenfreqncycnt = 0
# Loop in the freqncyDictionary using the For loop.
for elemnt in freqncyDictionary:
    # Check if the key in the freqncyDictionary having value even
    # using the if conditional statement.
    if(freqncyDictionary[elemnt] % 2 == 0):
                # If it is true then increment the evenfreqncycnt by the key
        # and store it in the same variable.
        evenfreqncycnt += elemnt
# After the end of For loop then print the evenfreqncycnt value.
print('The sum of all even frequency elements in the given list', gvnlst, 'is:')
print(evenfreqncycnt)

Output:

The sum of all even frequency elements in the given list [10, 4, 2, 10, 1, 2, 3, 4, 5, 6, 6] is:
22

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)
  • Take a variable say evenfreqncycnt and initialize its value to 0.
  • Loop in the freqncyDictionary using the For loop.
  • Check if the key in the freqncyDictionary having value even using the if conditional statement.
  • If it is true then increment the evenfreqncycnt by the key and store it in the same variable.
  • After the end of For loop then print the evenfreqncycnt value.
  • 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 say evenfreqncycnt and initialize its value to 0.
evenfreqncycnt = 0
# Loop in the freqncyDictionary using the For loop.
for elemnt in freqncyDictionary:
    # Check if the key in the freqncyDictionary having value even
    # using the if conditional statement.
    if(freqncyDictionary[elemnt] % 2 == 0):
                # If it is true then increment the evenfreqncycnt by the key
        # and store it in the same variable.
        evenfreqncycnt += elemnt
# After the end of For loop then print the evenfreqncycnt value.
print('The sum of all even frequency elements in the given list', gvnlst, 'is:')
print(evenfreqncycnt)

Output:

Enter some random list element separated by spaces = 3 4 1 2 4 1 3 
The sum of all even frequency elements in the given list [3, 4, 1, 2, 4, 1, 3] is:
8

If you are learning Python then the Python Programming Example is for you and gives you a thorough description of concepts for beginners, experienced programmers.