Find the sum of all highest occurring elements in an Array

Given an array of integers containing duplicate elements. The task is to find the sum of all highest occurring elements in the given array. That is the sum of all such elements whose frequency is maximum in the array.
Examples:Â
Â
Input : arr[] = {1, 1, 2, 2, 2, 2, 3, 3, 3, 3}
Output : 20
The highest occurring elements are 3 and 2 and their
frequency is 4. Therefore sum of all 3's and 2's in the
array = 3+3+3+3+2+2+2+2 = 20.
Input : arr[] = {10, 20, 30, 40, 40}
Output : 80
Â
Approach:Â
Â
- Traverse the array and use a unordered_map in C++ to store the frequency of elements of the array such that the key of map is the array element and value is its frequency in the array.
- Then, traverse the map to find the frequency of the max occurring element.
- Now, to find the sum traverse the map again and for all elements with maximum frequency find frequency_of_max_occurring_element*max_occurring_element and find their sum.
Below is the implementation of the above approach:Â
Â
C++
// CPP program to find the sum of all maximum// occurring elements in an arrayÂ
#include <bits/stdc++.h>using namespace std;Â
// Function to find the sum of all maximum// occurring elements in an arrayint findSum(int arr[], int N){    // Store frequencies of elements    // of the array    unordered_map<int, int> mp;    for (int i = 0; i < N; i++)         mp[arr[i]]++;     Â
    // Find the max frequency    int maxFreq = 0;    for (auto itr = mp.begin(); itr != mp.end(); itr++) {        if (itr->second > maxFreq) {            maxFreq = itr->second;        }    }Â
    // Traverse the map again and find the sum    int sum = 0;    for (auto itr = mp.begin(); itr != mp.end(); itr++) {        if (itr->second == maxFreq) {            sum += itr->first * itr->second;        }    }Â
    return sum;}Â
// Driver Codeint main(){Â Â Â Â int arr[] = { 1, 1, 2, 2, 2, 2, 3, 3, 3, 3 };Â
    int N = sizeof(arr) / sizeof(arr[0]);Â
    cout << findSum(arr, N);Â
    return 0;} |
Java
// Java program to find the sum of all maximum// occurring elements in an arrayimport java.util.*;Â
class GFG {Â
// Function to find the sum of all maximum// occurring elements in an arraystatic int findSum(int arr[], int N){    // Store frequencies of elements    // of the array    Map<Integer,Integer> mp = new HashMap<>();    for (int i = 0 ; i < N; i++)    {        if(mp.containsKey(arr[i]))        {            mp.put(arr[i], mp.get(arr[i])+1);        }        else        {            mp.put(arr[i], 1);        }    }Â
    // Find the max frequency    int maxFreq = 0;    for (Map.Entry<Integer,Integer> entry : mp.entrySet())     {        if (entry.getValue() > maxFreq)         {            maxFreq = entry.getValue();        }    }Â
    // Traverse the map again and find the sum    int sum = 0;    for (Map.Entry<Integer,Integer> entry : mp.entrySet())     {        if (entry.getValue() == maxFreq)         {            sum += entry.getKey() * entry.getValue();        }    }Â
    return sum;}Â
// Driver Codepublic static void main(String[] args){Â
    int arr[] = { 1, 1, 2, 2, 2, 2, 3, 3, 3, 3 };Â
    int N = arr.length;    System.out.println(findSum(arr, N));}}Â
// This code is contributed by Princi Singh |
Python3
# Python3 program to find the Sum of all maximum# occurring elements in an arrayÂ
# Function to find the Sum of all maximum# occurring elements in an arraydef findSum(arr, N):         # Store frequencies of elements    # of the array    mp = dict()    for i in range(N):         mp[arr[i]] = mp.get(arr[i], 0) + 1     Â
    # Find the max frequency    maxFreq = 0    for itr in mp:        if (mp[itr] > maxFreq):            maxFreq = mp[itr]         Â
    # Traverse the map again and find the Sum    Sum = 0    for itr in mp:        if (mp[itr] == maxFreq):            Sum += itr* mp[itr]         return SumÂ
Â
# Driver CodeÂ
arr= [1, 1, 2, 2, 2, 2, 3, 3, 3, 3 ]Â
N = len(arr)Â
print(findSum(arr, N))Â
# This code is contributed by mohit kumar |
C#
// C# program to find the sum of all maximum// occurring elements in an arrayusing System;using System.Collections.Generic; Â
class GFG {Â
// Function to find the sum of all maximum// occurring elements in an arraystatic int findSum(int []arr, int N){    // Store frequencies of elements    // of the array    Dictionary<int,int> mp = new Dictionary<int,int>();    for (int i = 0 ; i < N; i++)    {        if(mp.ContainsKey(arr[i]))        {            var val = mp[arr[i]];            mp.Remove(arr[i]);            mp.Add(arr[i], val + 1);         }        else        {            mp.Add(arr[i], 1);        }    }Â
    // Find the max frequency    int maxFreq = 0;    foreach(KeyValuePair<int, int> entry in mp)     {        if (entry.Value > maxFreq)         {            maxFreq = entry.Value;        }    }Â
    // Traverse the map again and find the sum    int sum = 0;    foreach(KeyValuePair<int, int> entry in mp)    {        if (entry.Value == maxFreq)         {            sum += entry.Key * entry.Value;        }    }Â
    return sum;}Â
// Driver Codepublic static void Main(String[] args){Â
    int []arr = { 1, 1, 2, 2, 2, 2, 3, 3, 3, 3 };Â
    int N = arr.Length;    Console.WriteLine(findSum(arr, N));}}Â
// This code is contributed by Rajput-Ji |
Javascript
<script>Â
// JavaScript program to find // the sum of all maximum// occurring elements in an arrayÂ
// Function to find the sum of all maximum// occurring elements in an arrayfunction findSum(arr,N){    // Store frequencies of elements    // of the array    let mp = new Map();    for (let i = 0 ; i < N; i++)    {        if(mp.has(arr[i]))        {            mp.set(arr[i], mp.get(arr[i])+1);        }        else        {            mp.set(arr[i], 1);        }    }       // Find the max frequency    let maxFreq = 0;    for (let [key, value] of mp.entries())     {        if (value > maxFreq)         {            maxFreq = value;        }    }       // Traverse the map again and find the sum    let sum = 0;    for (let [key, value] of mp.entries())     {        if (value == maxFreq)         {            sum += key * value;        }    }       return sum;}Â
// Driver CodeÂ
let arr=[ 1, 1, 2, 2, 2, 2, 3, 3, 3, 3 ];let N = arr.length;document.write(findSum(arr, N));Â
Â
// This code is contributed by patel2127Â
</script> |
Output:Â
20
Â
Time Complexity: O(N), where N is the number of elements in the array.
Auxiliary Space: O(N) because it is using unordered_map
Â
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



