Sum of all minimum occurring elements in an Array

Given an array of integers containing duplicate elements. The task is to find the sum of all least occurring elements in the given array. That is the sum of all such elements whose frequency is minimum in the array.

Examples:

Input : arr[] = {1, 1, 2, 2, 3, 3, 3, 3}
Output : 2
The least occurring element is 1 and 2 and it's number
of occurrence is 2. Therefore sum of all 1's and 2's in the 
array = 1+1+2+2 = 6.

Input : arr[] = {10, 20, 30, 40, 40}
Output : 60
Elements with least frequency are 10, 20, 30.
Their sum = 10 + 20 + 30 = 60.

Approach:



Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to find the sum of all minimum
// occurring elements in an array
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to find the sum of all minimum
// occurring elements in an array
int 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 min frequency
    int minFreq = INT_MAX;
    for (auto itr = mp.begin(); itr != mp.end(); itr++) {
        if (itr->second < minFreq) {
            minFreq = 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 == minFreq) {
            sum += itr->first * itr->second;
        }
    }
  
    return sum;
}
  
// Driver Code
int main()
{
    int arr[] = { 10, 20, 30, 40, 40 };
  
    int N = sizeof(arr) / sizeof(arr[0]);
  
    cout << findSum(arr, N);
  
    return 0;
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find the sum of all minimum
// occurring elements in an array
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
  
class GFG 
// Function to find the sum of all minimum
// occurring elements in an array
static 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++) 
        mp.put(arr[i],mp.get(arr[i])==null?1:mp.get(arr[i])+1);
  
  
    // Find the min frequency
    int minFreq = Integer.MAX_VALUE;
    minFreq = Collections.min(mp.entrySet(), 
            Comparator.comparingInt(Map.Entry::getKey)).getValue();
  
  
    // Traverse the map again and find the sum
    int sum = 0;
    for (Map.Entry<Integer,Integer> entry : mp.entrySet()) 
    {
        if (entry.getValue() == minFreq)
        {
            sum += entry.getKey() * entry.getValue();
        }
    }
  
    return sum;
}
  
// Driver Code
public static void main(String[] args) 
{
    int arr[] = { 10, 20, 30, 40, 40 };
  
    int N = arr.length;
  
    System.out.println( findSum(arr, N));
}
}
  
// This code contributed by Rajput-Ji
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find theSum of all 
# minimum occurring elements in an array
import math as mt
  
# Function to find theSum of all minimum
# occurring elements in an array
def findSum(arr, N):
  
    # Store frequencies of elements
    # of the array
    mp = dict()
    for i in arr:
        if i in mp.keys():
            mp[i] += 1
        else:
            mp[i] = 1
  
    # Find the min frequency
    minFreq = 10**9
    for itr in mp:
        if mp[itr]< minFreq:
            minFreq = mp[itr]
          
    # Traverse the map again and 
    # find theSum
    Sum = 0
    for itr in mp:
        if mp[itr]== minFreq:
            Sum += itr * mp[itr]
          
    return Sum
  
# Driver Code
arr = [ 10, 20, 30, 40, 40
  
N = len(arr)
  
print(findSum(arr, N))
  
# This code is contributed by
# mohit kumar 29
chevron_right

Output:
60

Time Complexity: O(N), where N is the number of elements in the array.

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details





Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : mohit kumar 29, Rajput-Ji

Article Tags :
Practice Tags :