给定一个包含重复元素的整数数组。任务是找到给定数组中所有偶数出现的元素的总和。那是数组中频率为偶数的所有此类元素的总和。
例子:
Input : arr[] = {1, 1, 2, 2, 3, 3, 3}
Output : 6
The even occurring element are 1 and 2 (both occur two times).
Therefore sum of all 1's in the array = 1+1+2+2 = 6.
Input : arr[] = {10, 20, 30, 40, 40}
Output : 80
Element with even frequency are 40.
Sum = 40.
方法:
- 遍历数组并使用C++中的unordered_map来存储数组元素的频率,这样map的key就是数组元素,value就是它在数组中的频率。
- 然后,遍历map,查找元素出现的频率并检查是否为偶数,如果为偶数,则将此元素添加到sum中。
下面是上述方法的实现:
C++
// C++ program to find the sum of all even
// occurring elements in an array
#include
using namespace std;
// Function to find the sum of all even
// occurring elements in an array
int findSum(int arr[], int N)
{
// Map to store frequency of elements
// of the array
unordered_map mp;
for (int i = 0; i < N; i++) {
mp[arr[i]]++;
}
// variable to store sum of all
// even occurring elements
int sum = 0;
// loop to iterate through map
for (auto itr = mp.begin(); itr != mp.end(); itr++) {
// check if frequency is even
if (itr->second % 2 == 0)
sum += (itr->first) * (itr->second);
}
return sum;
}
// Driver Code
int main()
{
int arr[] = { 10, 20, 20, 40, 40 };
int N = sizeof(arr) / sizeof(arr[0]);
cout << findSum(arr, N);
return 0;
}
Java
// Java program to find the sum of all even
// occurring elements in an array
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
public class GFG {
public static int element(int[] arr, int n)
{
Map map = new HashMap();
for (int i = 0; i < n; i++) {
int count = 0;
if (map.get(arr[i]) != null) {
count = map.get(arr[i]);
}
map.put(arr[i], count + 1);
}
int sum = 0;
for (Entry entry : map.entrySet()) {
if (entry.getValue() % 2 == 0) {
sum += entry.getKey() * entry.getValue();
}
}
return sum;
}
public static void main(String[] args)
{
int arr[] = { 1, 1, 2, 2, 3, 3, 3 };
// sum should be = 1+1+2+2=6;
int n = arr.length;
System.out.println(element(arr, n));
}
}
Python3
# Python3 program to find the sum
# of all even occurring elements
# in an array
# Function to find the sum of all even
# occurring elements in an array
def findSum(arr, N):
# Map to store frequency of
# elements of the array
mp = {}
for i in range(N):
if arr[i] in mp:
mp[arr[i]] += 1
else:
mp[arr[i]] = 1
# Variable to store sum of all
# even occurring elements
Sum = 0
# Loop to iterate through map
for first, second in mp.items():
# Check if frequency is even
if (second % 2 == 0):
Sum += (first) * (second)
return Sum
# Driver code
arr = [ 10, 20, 20, 40, 40 ]
N = len(arr)
print(findSum(arr, N))
# This code is contributed by divyeshrabadiya07
C#
// C# program to find the sum of all even
// occurring elements in an array
using System;
using System.Collections.Generic;
class GFG {
static int element(int[] arr, int n)
{
Dictionary map = new Dictionary();
for (int i = 0; i < n; i++)
{
if(!map.ContainsKey(arr[i]))
{
map.Add(arr[i], 1);
}
else
{
map[arr[i]] += 1;
}
}
int sum = 0;
foreach(KeyValuePair entry in map)
{
if (entry.Value % 2 == 0)
{
sum += entry.Key * entry.Value;
}
}
return sum;
}
// Driver code
static void Main() {
int[] arr = { 10, 20, 20, 40, 40};
int n = arr.Length;
Console.WriteLine(element(arr, n));
}
}
// This code is contributed by divyesh072019
Javascript
Python3
# Python3 implementation
from collections import Counter
def sumEven(arr, n):
# Counting frequency of every
# element using Counter
freq = Counter(arr)
# initializing sum 0
sum = 0
# Traverse the freq and print all
# sum all elements with even frequency
# multiplied by its frequency
for it in freq:
if freq[it] % 2 == 0:
sum = sum + it*freq[it]
print(sum)
# Driver code
arr = [10, 20, 20, 40, 40]
n = len(arr)
sumEven(arr, n)
# This code is contributed by vikkycirus
输出:
120
时间复杂度:O(N),其中 N 是数组中元素的数量。
方法2:使用内置Python函数:
- 使用 Counter函数计算每个元素的频率
- 遍历频率字典并求和所有出现偶数与其频率相乘的元素。
下面是实现:
蟒蛇3
# Python3 implementation
from collections import Counter
def sumEven(arr, n):
# Counting frequency of every
# element using Counter
freq = Counter(arr)
# initializing sum 0
sum = 0
# Traverse the freq and print all
# sum all elements with even frequency
# multiplied by its frequency
for it in freq:
if freq[it] % 2 == 0:
sum = sum + it*freq[it]
print(sum)
# Driver code
arr = [10, 20, 20, 40, 40]
n = len(arr)
sumEven(arr, n)
# This code is contributed by vikkycirus
输出:
120
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。