# Sum of all minimum occurring elements in an Array

Here given code implementation process.

```
import java.util.HashMap;
/*
Java program for
Sum of all minimum occurring elements in an Array
*/
public class Occurrence
{
public void minOccurrenceSum(int[] arr, int n)
{
int sum = 0;
int min = n;
HashMap < Integer, Integer > record =
new HashMap < Integer, Integer > ();
// Count frequency of array elements
for (int i = 0; i < n; ++i)
{
if (record.containsKey(arr[i]))
{
// Increase the frequency
record.put(arr[i], record.get(arr[i]) + 1);
}
else
{
// Get most occuring element frequency
record.put(arr[i], 1);
}
}
// Find minimum frequency of array elements
for (int v: record.keySet())
{
if (record.get(v) < min)
{
min = record.get(v);
}
}
// Sum of elements which are appear minimum times
for (int v: record.keySet())
{
if (record.get(v) == min)
{
// Sum of minimum occurring element
sum += (v * min);
}
}
// Display sum
System.out.println(" Result : " + sum);
}
public static void main(String[] args)
{
Occurrence task = new Occurrence();
int[] arr = {
6 , 2 , 4 , 6 , -1 , 6 , 8 , 8 , 8 , 6 , -1 , 2 , 4
};
int n = arr.length;
/*
[2+2] = 4
[4+4] = 8
[-1+(-1)] = -2
------------------------------
Result 10
-----------------------------
Note minimum occuring element frequency are 2
*/
task.minOccurrenceSum(arr, n);
}
}
```

#### Output

` Result : 10`

```
// Include header file
#include <iostream>
#include <unordered_map>
using namespace std;
/*
C++ program for
Sum of all minimum occurring elements in an Array
*/
class Occurrence
{
public: void minOccurrenceSum(int arr[], int n)
{
int sum = 0;
int min = n;
unordered_map < int, int > record;
// Count frequency of array elements
for (int i = 0; i < n; ++i)
{
if (record.find(arr[i]) != record.end())
{
// Increase the frequency
record[arr[i]] = record[arr[i]] + 1;
}
else
{
// Get most occuring element frequency
record[arr[i]] = 1;
}
}
// Find minimum frequency of array elements
for (auto &v: record)
{
if (record[v.first] < min)
{
min = record[v.first];
}
}
// Sum of elements which are appear minimum times
for (auto &v: record)
{
if (record[v.first] == min)
{
// Sum of minimum occurring element
sum += (v.first *min);
}
}
// Display sum
cout << " Result : " << sum << endl;
}
};
int main()
{
Occurrence *task = new Occurrence();
int arr[] = {
6 , 2 , 4 , 6 , -1 , 6 , 8 , 8 , 8 , 6 , -1 , 2 , 4
};
int n = sizeof(arr) / sizeof(arr[0]);
/*
[2+2] = 4
[4+4] = 8
[-1+(-1)] = -2
------------------------------
Result 10
-----------------------------
Note minimum occuring element frequency are 2
*/
task->minOccurrenceSum(arr, n);
return 0;
}
```

#### Output

` Result : 10`

```
// Include namespace system
using System;
using System.Collections.Generic;
/*
Csharp program for
Sum of all minimum occurring elements in an Array
*/
public class Occurrence
{
public void minOccurrenceSum(int[] arr, int n)
{
int sum = 0;
int min = n;
Dictionary < int, int > record =
new Dictionary < int, int > ();
// Count frequency of array elements
for (int i = 0; i < n; ++i)
{
if (record.ContainsKey(arr[i]))
{
// Increase the frequency
record[arr[i]] = record[arr[i]] + 1;
}
else
{
// Get most occuring element frequency
record.Add(arr[i], 1);
}
}
// Find minimum frequency of array elements
foreach(KeyValuePair < int, int > v in record)
{
if (v.Value < min)
{
min = v.Value;
}
}
// Sum of elements which are appear minimum times
foreach(KeyValuePair < int, int > v in record)
{
if (v.Value == min)
{
// Sum of minimum occurring element
sum += (v.Key * min);
}
}
// Display sum
Console.WriteLine(" Result : " + sum);
}
public static void Main(String[] args)
{
Occurrence task = new Occurrence();
int[] arr = {
6 , 2 , 4 , 6 , -1 , 6 , 8 ,
8 , 8 , 6 , -1 , 2 , 4
};
int n = arr.Length;
/*
[2+2] = 4
[4+4] = 8
[-1+(-1)] = -2
------------------------------
Result 10
-----------------------------
Note minimum occuring element frequency are 2
*/
task.minOccurrenceSum(arr, n);
}
}
```

#### Output

` Result : 10`

```
package main
import "fmt"
/*
Go program for
Sum of all minimum occurring elements in an Array
*/
func minOccurrenceSum(arr[] int, n int) {
var sum int = 0
var min int = n
var record = make(map[int] int)
// Count frequency of array elements
for i := 0 ; i < n ; i++ {
if _, found := record[arr[i]] ; found {
// Increase the frequency
record[arr[i]] = record[arr[i]] + 1
} else {
// Get most occuring element frequency
record[arr[i]] = 1
}
}
// Find minimum frequency of array elements
for _, v := range record {
if v < min {
min = v
}
}
// Sum of elements which are appear minimum times
for k, v := range record {
if v == min {
// Sum of minimum occurring element
sum += (k * min)
}
}
// Display sum
fmt.Println(" Result : ", sum)
}
func main() {
var arr = [] int {6 , 2 , 4 , 6 , -1 , 6 ,
8 , 8 , 8 , 6 , -1 , 2 , 4}
var n int = len(arr)
/*
[2+2] = 4
[4+4] = 8
[-1+(-1)] = -2
------------------------------
Result 10
-----------------------------
Note minimum occuring element frequency are 2
*/
minOccurrenceSum(arr, n)
}
```

#### Output

` Result : 10`

```
<?php
/*
Php program for
Sum of all minimum occurring elements in an Array
*/
class Occurrence
{
public function minOccurrenceSum($arr, $n)
{
$sum = 0;
$min = $n;
$record = array();
// Count frequency of array elements
for ($i = 0; $i < $n; ++$i)
{
if (array_key_exists($arr[$i], $record))
{
// Increase the frequency
$record[$arr[$i]] = $record[$arr[$i]] + 1;
}
else
{
// Get most occuring element frequency
$record[$arr[$i]] = 1;
}
}
// Find minimum frequency of array elements
foreach($record as $key => $value)
{
if ($value < $min)
{
$min = $value;
}
}
// Sum of elements which are appear minimum times
foreach($record as $key => $value)
{
if ($value == $min)
{
// Sum of minimum occurring element
$sum += ($key * $min);
}
}
// Display sum
echo(" Result : ".$sum."\n");
}
}
function main()
{
$task = new Occurrence();
$arr = array(6, 2, 4, 6, -1, 6, 8, 8, 8, 6, -1, 2, 4);
$n = count($arr);
/*
[2+2] = 4
[4+4] = 8
[-1+(-1)] = -2
------------------------------
Result 10
-----------------------------
Note minimum occuring element frequency are 2
*/
$task->minOccurrenceSum($arr, $n);
}
main();
```

#### Output

` Result : 10`

```
/*
Node JS program for
Sum of all minimum occurring elements in an Array
*/
class Occurrence
{
minOccurrenceSum(arr, n)
{
var sum = 0;
var min = n;
var record = new Map();
// Count frequency of array elements
for (var i = 0; i < n; ++i)
{
if (record.has(arr[i]))
{
// Increase the frequency
record.set(arr[i], record.get(arr[i]) + 1);
}
else
{
// Get most occuring element frequency
record.set(arr[i], 1);
}
}
// Find minimum frequency of array elements
for (let [key, value] of record)
{
if (value < min)
{
min = value;
}
}
// Sum of elements which are appear minimum times
for (let [key, value] of record)
{
if (value == min)
{
// Sum of minimum occurring element
sum += (key * min);
}
}
// Display sum
console.log(" Result : " + sum);
}
}
function main()
{
var task = new Occurrence();
var arr = [6, 2, 4, 6, -1, 6, 8, 8, 8, 6, -1, 2, 4];
var n = arr.length;
/*
[2+2] = 4
[4+4] = 8
[-1+(-1)] = -2
------------------------------
Result 10
-----------------------------
Note minimum occuring element frequency are 2
*/
task.minOccurrenceSum(arr, n);
}
main();
```

#### Output

` Result : 10`

```
# Python 3 program for
# Sum of all minimum occurring elements in an Array
class Occurrence :
def minOccurrenceSum(self, arr, n) :
sum = 0
min = n
record = dict()
i = 0
# Count frequency of list elements
while (i < n) :
if ((arr[i] in record.keys())) :
# Increase the frequency
record[arr[i]] = record.get(arr[i]) + 1
else :
# Get most occuring element frequency
record[arr[i]] = 1
i += 1
for key, value in record.items() :
if (value < min) :
min = value
for key, value in record.items() :
if (value == min) :
# Sum of minimum occurring element
sum += (key * min)
# Display sum
print(" Result : ", sum)
def main() :
task = Occurrence()
arr = [6, 2, 4, 6, -1, 6, 8, 8, 8, 6, -1, 2, 4]
n = len(arr)
# [2+2] = 4
# [4+4] = 8
# [-1+(-1)] = -2
# ------------------------------
# Result 10
# -----------------------------
# Note minimum occuring element frequency are 2
task.minOccurrenceSum(arr, n)
if __name__ == "__main__": main()
```

#### Output

` Result : 10`

```
# Ruby program for
# Sum of all minimum occurring elements in an Array
class Occurrence
def minOccurrenceSum(arr, n)
sum = 0
min = n
record = Hash.new()
i = 0
# Count frequency of array elements
while (i < n)
if (record.key?(arr[i]))
# Increase the frequency
record[arr[i]] = record[arr[i]] + 1
else
# Get most occuring element frequency
record[arr[i]] = 1
end
i += 1
end
# Find minimum frequency of array elements
record.each { | key, value |
if (value < min)
min = value
end
}
# Sum of elements which are appear minimum times
record.each { | key, value |
if (value == min)
# Sum of minimum occurring element
sum += (key * min)
end
}
# Display sum
print(" Result : ", sum, "\n")
end
end
def main()
task = Occurrence.new()
arr = [6, 2, 4, 6, -1, 6, 8, 8, 8, 6, -1, 2, 4]
n = arr.length
# [2+2] = 4
# [4+4] = 8
# [-1+(-1)] = -2
# ------------------------------
# Result 10
# -----------------------------
# Note minimum occuring element frequency are 2
task.minOccurrenceSum(arr, n)
end
main()
```

#### Output

```
Result : 10
```

```
import scala.collection.mutable._;
/*
Scala program for
Sum of all minimum occurring elements in an Array
*/
class Occurrence()
{
def minOccurrenceSum(arr: Array[Int], n: Int): Unit = {
var sum: Int = 0;
var min: Int = n;
var record: HashMap[Int, Int] = new HashMap[Int, Int]();
var i: Int = 0;
// Count frequency of array elements
while (i < n)
{
if (record.contains(arr(i)))
{
// Increase the frequency
record.addOne(arr(i), record.get(arr(i)).get + 1);
}
else
{
// Get most occuring element frequency
record.addOne(arr(i), 1);
}
i += 1;
}
// Find minimum frequency of array elements
for ((key, value) <- record)
{
if (value < min)
{
min = value;
}
}
// Sum of elements which are appear minimum times
for ((key, value) <- record)
{
if (value == min)
{
// Sum of minimum occurring element
sum += (key * min);
}
}
// Display sum
println(" Result : " + sum);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Occurrence = new Occurrence();
var arr: Array[Int] =
Array(6, 2, 4, 6, -1, 6, 8, 8, 8, 6, -1, 2, 4);
var n: Int = arr.length;
/*
[2+2] = 4
[4+4] = 8
[-1+(-1)] = -2
------------------------------
Result 10
-----------------------------
Note minimum occuring element frequency are 2
*/
task.minOccurrenceSum(arr, n);
}
}
```

#### Output

` Result : 10`

```
import Foundation;
/*
Swift 4 program for
Sum of all minimum occurring elements in an Array
*/
class Occurrence
{
func minOccurrenceSum(_ arr: [Int], _ n: Int)
{
var sum: Int = 0;
var min: Int = n;
var record = [Int : Int]();
var i: Int = 0;
// Count frequency of array elements
while (i < n)
{
if (record.keys.contains(arr[i]))
{
// Increase the frequency
record[arr[i]] = record[arr[i]]! + 1;
}
else
{
// Get most occuring element frequency
record[arr[i]] = 1;
}
i += 1;
}
// Find minimum frequency of array elements
for (_, value) in record
{
if (value < min)
{
min = value;
}
}
// Sum of elements which are appear minimum times
for (key, value) in record
{
if (value == min)
{
// Sum of minimum occurring element
sum += (key * min);
}
}
// Display sum
print(" Result : ", sum);
}
}
func main()
{
let task: Occurrence = Occurrence();
let arr: [Int] = [6, 2, 4, 6, -1, 6, 8, 8, 8, 6, -1, 2, 4];
let n: Int = arr.count;
/*
[2+2] = 4
[4+4] = 8
[-1+(-1)] = -2
------------------------------
Result 10
-----------------------------
Note minimum occuring element frequency are 2
*/
task.minOccurrenceSum(arr, n);
}
main();
```

#### Output

` Result : 10`

```
/*
Kotlin program for
Sum of all minimum occurring elements in an Array
*/
class Occurrence
{
fun minOccurrenceSum(arr: Array < Int > , n: Int): Unit
{
var sum: Int = 0;
var min: Int = n;
val record = HashMap < Int, Int > ();
var i: Int = 0;
// Count frequency of array elements
while (i < n)
{
if (record.containsKey(arr[i]))
{
// Increase the frequency
record.put(arr[i], record.getValue(arr[i]) + 1);
}
else
{
// Get most occuring element frequency
record.put(arr[i], 1);
}
i += 1;
}
// Find minimum frequency of array elements
for ((_, value) in record)
{
if (value < min)
{
min = value;
}
}
// Sum of elements which are appear minimum times
for ((key, value) in record)
{
if (value == min)
{
// Sum of minimum occurring element
sum += (key * min);
}
}
// Display sum
println(" Result : " + sum);
}
}
fun main(args: Array < String > ): Unit
{
val task: Occurrence = Occurrence();
val arr: Array < Int > = arrayOf(
6, 2, 4, 6, -1, 6, 8, 8, 8, 6, -1, 2, 4
);
val n: Int = arr.count();
/*
[2+2] = 4
[4+4] = 8
[-1+(-1)] = -2
------------------------------
Result 10
-----------------------------
Note minimum occuring element frequency are 2
*/
task.minOccurrenceSum(arr, n);
}
```

#### Output

` Result : 10`

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

## New Comment