Sum of all minimum occurring elements in an Array
Last Updated :
05 Sep, 2022
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:
- 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 minimum occurring element.
- Now, to find the sum traverse the map again and for all elements with minimum frequency find frequency_of_min_occurring_element*min_occurring_element and find their sum.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findSum( int arr[], int N)
{
unordered_map< int , int > mp;
for ( int i = 0; i < N; i++)
mp[arr[i]]++;
int minFreq = INT_MAX;
for ( auto itr = mp.begin(); itr != mp.end(); itr++) {
if (itr->second < minFreq) {
minFreq = itr->second;
}
}
int sum = 0;
for ( auto itr = mp.begin(); itr != mp.end(); itr++) {
if (itr->second == minFreq) {
sum += itr->first * itr->second;
}
}
return sum;
}
int main()
{
int arr[] = { 10, 20, 30, 40, 40 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << findSum(arr, N);
return 0;
}
|
Java
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
class GFG
{
static int findSum( int arr[], int N)
{
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 );
int minFreq = Integer.MAX_VALUE;
minFreq = Collections.min(mp.entrySet(),
Comparator.comparingInt(Map.Entry::getKey)).getValue();
int sum = 0 ;
for (Map.Entry<Integer,Integer> entry : mp.entrySet())
{
if (entry.getValue() == minFreq)
{
sum += entry.getKey() * entry.getValue();
}
}
return sum;
}
public static void main(String[] args)
{
int arr[] = { 10 , 20 , 30 , 40 , 40 };
int N = arr.length;
System.out.println( findSum(arr, N));
}
}
|
Python3
import math as mt
def findSum(arr, N):
mp = dict ()
for i in arr:
if i in mp.keys():
mp[i] + = 1
else :
mp[i] = 1
minFreq = 10 * * 9
for itr in mp:
if mp[itr]< minFreq:
minFreq = mp[itr]
Sum = 0
for itr in mp:
if mp[itr] = = minFreq:
Sum + = itr * mp[itr]
return Sum
arr = [ 10 , 20 , 30 , 40 , 40 ]
N = len (arr)
print (findSum(arr, N))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int findSum( int [] arr, int N)
{
Dictionary< int ,
int > mp = new Dictionary< int ,
int >();
for ( int i = 0; i < N; i++)
{
if (mp.ContainsKey(arr[i]))
{
mp[arr[i]]++;
}
else
{
mp.Add(arr[i], 1);
}
}
int minFreq = Int32.MaxValue;
foreach (KeyValuePair< int , int > itr in mp)
{
if (itr.Value < minFreq)
{
minFreq = itr.Value;
}
}
int sum = 0;
foreach (KeyValuePair< int , int > itr in mp)
{
if (itr.Value == minFreq)
{
sum += itr.Key * itr.Value;
}
}
return sum;
}
static void Main()
{
int [] arr = { 10, 20, 30, 40, 40 };
int N = arr.Length;
Console.Write(findSum(arr, N));
}
}
|
Javascript
<script>
function findSum(arr,N)
{
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);
}
}
let minFreq = Number.MAX_VALUE;
for (let [key, value] of mp.entries())
{
if (value < minFreq)
{
minFreq = value;
}
}
let sum = 0;
for (let [key, value] of mp.entries())
{
if (value == minFreq)
{
sum += key * value;
}
}
return sum;
}
let arr=[ 10, 20, 30, 40, 40 ];
let N = arr.length;
document.write(findSum(arr, N));
</script>
|
Time Complexity: O(N), where N is the number of elements in the array.
Auxiliary Space: O(N) because it is using unordered_map “mp”
Share your thoughts in the comments
Please Login to comment...