{"id":20618,"date":"2021-09-21T08:59:20","date_gmt":"2021-09-21T03:29:20","guid":{"rendered":"https:\/\/python-programs.com\/?p=20618"},"modified":"2021-11-22T18:36:18","modified_gmt":"2021-11-22T13:06:18","slug":"python-program-to-find-the-sum-of-all-even-occurring-elements-in-an-array-list","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-the-sum-of-all-even-occurring-elements-in-an-array-list\/","title":{"rendered":"Python Program to Find the Sum of all Even Occurring Elements in an Array\/List"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Find the sum of all Highest Occurring Elements in an Array\/List<\/a><\/p>\n Given a list, the task is to find the sum of the elements which are having an even frequency in the given array\/List.<\/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 the elements which are having an even frequency in the given array\/List:<\/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 If you are learning Python then the Python Programming Example<\/a> is for you and gives you a thorough description of concepts for beginners, experienced programmers.<\/p>\n 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, …<\/p>\nGiven List = [6, 1, 4, 1, 1, 6, 4, 4, 2, 2]<\/pre>\n
The sum of all even frequency elements in the given list [6, 1, 4, 1, 1, 6, 4, 4, 2, 2] is:\r\n8<\/pre>\n
Here 6,2 are the elements in the given list which are having even frequency\r\nsum=6+2=8<\/pre>\n
Given List = [7, 6, 8, 8, 1, 4, 1, 5, 4, 7]<\/pre>\n
The sum of all even frequency elements in the given list [7, 6, 8, 8, 1, 4, 1, 5, 4, 7] is:\r\n20<\/pre>\n
Program to Find the Sum of all Even 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 = [6, 1, 4, 1, 1, 6, 4, 4, 2, 2]\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\r\n# Take a variable say evenfreqncycnt and initialize its value to 0.\r\nevenfreqncycnt = 0\r\n# Loop in the freqncyDictionary using the For loop.\r\nfor elemnt in freqncyDictionary:\r\n # Check if the key in the freqncyDictionary having value even\r\n # using the if conditional statement.\r\n if(freqncyDictionary[elemnt] % 2 == 0):\r\n # If it is true then increment the evenfreqncycnt by the key\r\n # and store it in the same variable.\r\n evenfreqncycnt += elemnt\r\n# After the end of For loop then print the evenfreqncycnt value.\r\nprint('The sum of all even frequency elements in the given list', gvnlst, 'is:')\r\nprint(evenfreqncycnt)\r\n<\/pre>\n
The sum of all even frequency elements in the given list [6, 1, 4, 1, 1, 6, 4, 4, 2, 2] is:\r\n8<\/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\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# 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\r\n# Take a variable say evenfreqncycnt and initialize its value to 0.\r\nevenfreqncycnt = 0\r\n# Loop in the freqncyDictionary using the For loop.\r\nfor elemnt in freqncyDictionary:\r\n # Check if the key in the freqncyDictionary having value even\r\n # using the if conditional statement.\r\n if(freqncyDictionary[elemnt] % 2 == 0):\r\n # If it is true then increment the evenfreqncycnt by the key\r\n # and store it in the same variable.\r\n evenfreqncycnt += elemnt\r\n# After the end of For loop then print the evenfreqncycnt value.\r\nprint('The sum of all even frequency elements in the given list', gvnlst, 'is:')\r\nprint(evenfreqncycnt)\r\n<\/pre>\n
Enter some random list element separated by spaces = 1 2 3 4 5 1 3 4\r\nThe sum of all even frequency elements in the given list [1, 2, 3, 4, 5, 1, 3, 4] is:\r\n8<\/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 = [10, 4, 2, 10, 1, 2, 3, 4, 5, 6, 6]\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\r\n# Take a variable say evenfreqncycnt and initialize its value to 0.\r\nevenfreqncycnt = 0\r\n# Loop in the freqncyDictionary using the For loop.\r\nfor elemnt in freqncyDictionary:\r\n # Check if the key in the freqncyDictionary having value even\r\n # using the if conditional statement.\r\n if(freqncyDictionary[elemnt] % 2 == 0):\r\n # If it is true then increment the evenfreqncycnt by the key\r\n # and store it in the same variable.\r\n evenfreqncycnt += elemnt\r\n# After the end of For loop then print the evenfreqncycnt value.\r\nprint('The sum of all even frequency elements in the given list', gvnlst, 'is:')\r\nprint(evenfreqncycnt)\r\n<\/pre>\n
The sum of all even frequency elements in the given list [10, 4, 2, 10, 1, 2, 3, 4, 5, 6, 6] is:\r\n22<\/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\r\n# Take a variable say evenfreqncycnt and initialize its value to 0.\r\nevenfreqncycnt = 0\r\n# Loop in the freqncyDictionary using the For loop.\r\nfor elemnt in freqncyDictionary:\r\n # Check if the key in the freqncyDictionary having value even\r\n # using the if conditional statement.\r\n if(freqncyDictionary[elemnt] % 2 == 0):\r\n # If it is true then increment the evenfreqncycnt by the key\r\n # and store it in the same variable.\r\n evenfreqncycnt += elemnt\r\n# After the end of For loop then print the evenfreqncycnt value.\r\nprint('The sum of all even frequency elements in the given list', gvnlst, 'is:')\r\nprint(evenfreqncycnt)\r\n<\/pre>\n
Enter some random list element separated by spaces = 3 4 1 2 4 1 3 \r\nThe sum of all even frequency elements in the given list [3, 4, 1, 2, 4, 1, 3] is:\r\n8<\/pre>\n
\n