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 elements which are having an odd frequency in the given array/List.

Examples:

Example1:

Input:

Given List =  [10, 4, 2, 10, 1, 2, 3, 4, 5, 6, 6]

Output:

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

Explanation:

Here 1,3,5 are the elements in the given list which are having odd frequency
sum=1+3+5

Example2:

Input:

Given List = [2, 2, 3]

Output:

The sum of all odd frequency elements in the given list [2, 2, 3] is:
3

Program to Find the Sum of all Odd Frequency Elements in an Array/List

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

Output:

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

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

Output:

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

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

Output:

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

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

Output:

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

Leave a Comment