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