Find the sum of all highest occurring elements in an Array
Here given code implementation process.
import java.util.HashMap;
/*
Java program for
Find the sum of all highest occurring elements in an Array
*/
public class Occurrence
{
public void sumHighestOccurrence(int[] arr, int n)
{
int sum = 0;
int max = 1;
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);
if (record.get(arr[i]) > max)
{
// Get most occuring element frequency
max = record.get(arr[i]);
}
}
else
{
// Add new element
record.put(arr[i], 1);
}
}
for (int v: record.keySet())
{
if (record.get(v) == max)
{
// Add all occurrences
sum += (v * max);
}
}
//
System.out.println(" Result : " + sum);
}
public static void main(String[] args)
{
Occurrence task = new Occurrence();
int[] arr = {
8 , 6 , -3 , 8 , 3 , 1 , 5 , 1 , 1 , -2 , -3 , -3 , 8
};
int n = arr.length;
/*
[8 + 8 + 8] = 24
[(-3) + (-3) + (-3)] = -9
[1+1+1] = 3
------------------------------
Result 18
-----------------------------
Note most occuring element frequency are 3
*/
task.sumHighestOccurrence(arr, n);
}
}
Output
Result : 18
// Include header file
#include <iostream>
#include <unordered_map>
using namespace std;
/*
C++ program for
Find the sum of all highest occurring elements in an Array
*/
class Occurrence
{
public: void sumHighestOccurrence(int arr[], int n)
{
int sum = 0;
int max = 1;
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;
if (record[arr[i]] > max)
{
// Get most occuring element frequency
max = record[arr[i]];
}
}
else
{
// Add new element
record[arr[i]] = 1;
}
}
for (auto &v: record)
{
if (v.second == max)
{
// Add all occurrences
sum += (v.first * max);
}
}
//
cout << " Result : " << sum << endl;
}
};
int main()
{
Occurrence *task = new Occurrence();
int arr[] = {
8 , 6 , -3 , 8 , 3 , 1 , 5 , 1 , 1 , -2 , -3 , -3 , 8
};
int n = sizeof(arr) / sizeof(arr[0]);
/*
[8 + 8 + 8] = 24
[(-3) + (-3) + (-3)] = -9
[1+1+1] = 3
------------------------------
Result 18
-----------------------------
Note most occuring element frequency are 3
*/
task->sumHighestOccurrence(arr, n);
return 0;
}
Output
Result : 18
// Include namespace system
using System;
using System.Collections.Generic;
/*
Csharp program for
Find the sum of all highest occurring elements in an Array
*/
public class Occurrence
{
public void sumHighestOccurrence(int[] arr, int n)
{
int sum = 0;
int max = 1;
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;
if (record[arr[i]] > max)
{
// Get most occuring element frequency
max = record[arr[i]];
}
}
else
{
// Add new element
record.Add(arr[i], 1);
}
}
foreach(KeyValuePair < int, int > v in record)
{
if (v.Value == max)
{
// Add all occurrences
sum += (v.Key * max);
}
}
//
Console.WriteLine(" Result : " + sum);
}
public static void Main(String[] args)
{
Occurrence task = new Occurrence();
int[] arr = {
8 , 6 , -3 , 8 , 3 , 1 , 5 , 1 , 1 , -2 , -3 , -3 , 8
};
int n = arr.Length;
/*
[8 + 8 + 8] = 24
[(-3) + (-3) + (-3)] = -9
[1+1+1] = 3
------------------------------
Result 18
-----------------------------
Note most occuring element frequency are 3
*/
task.sumHighestOccurrence(arr, n);
}
}
Output
Result : 18
package main
import "fmt"
/*
Go program for
Find the sum of all highest occurring elements in an Array
*/
func sumHighestOccurrence(arr[] int, n int) {
var sum int = 0
var max int = 1
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
if record[arr[i]] > max {
// Get most occuring element frequency
max = record[arr[i]]
}
} else {
// Add new element
record[arr[i]] = 1
}
}
for k, v := range record {
if v == max {
// Add all occurrences
sum += (k * max)
}
}
//
fmt.Println(" Result : ", sum)
}
func main() {
var arr = [] int {8, 6, -3, 8, 3, 1, 5, 1, 1, -2, -3, -3, 8}
var n int = len(arr)
/*
[8 + 8 + 8] = 24
[(-3) + (-3) + (-3)] = -9
[1+1+1] = 3
------------------------------
Result 18
-----------------------------
Note most occuring element frequency are 3
*/
sumHighestOccurrence(arr, n)
}
Output
Result : 18
<?php
/*
Php program for
Find the sum of all highest occurring elements in an Array
*/
class Occurrence
{
public function sumHighestOccurrence($arr, $n)
{
$sum = 0;
$max = 1;
$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;
if ($record[$arr[$i]] > $max)
{
// Get most occuring element frequency
$max = $record[$arr[$i]];
}
}
else
{
// Add new element
$record[$arr[$i]] = 1;
}
}
foreach($record as $key => $value)
{
if ($value == $max)
{
// Add all occurrences
$sum += ($key * $max);
}
}
//
echo(" Result : ".$sum."\n");
}
}
function main()
{
$task = new Occurrence();
$arr = array(8, 6, -3, 8, 3, 1, 5, 1, 1, -2, -3, -3, 8);
$n = count($arr);
/*
[8 + 8 + 8] = 24
[(-3) + (-3) + (-3)] = -9
[1+1+1] = 3
------------------------------
Result 18
-----------------------------
Note most occuring element frequency are 3
*/
$task->sumHighestOccurrence($arr, $n);
}
main();
Output
Result : 18
/*
Node JS program for
Find the sum of all highest occurring elements in an Array
*/
class Occurrence
{
sumHighestOccurrence(arr, n)
{
var sum = 0;
var max = 1;
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);
if (record.get(arr[i]) > max)
{
// Get most occuring element frequency
max = record.get(arr[i]);
}
}
else
{
// Add new element
record.set(arr[i], 1);
}
}
for (let [key, value] of record)
{
if (value == max)
{
// Add all occurrences
sum += (key * max);
}
}
//
console.log(" Result : " + sum);
}
}
function main()
{
var task = new Occurrence();
var arr = [8, 6, -3, 8, 3, 1, 5, 1, 1, -2, -3, -3, 8];
var n = arr.length;
/*
[8 + 8 + 8] = 24
[(-3) + (-3) + (-3)] = -9
[1+1+1] = 3
------------------------------
Result 18
-----------------------------
Note most occuring element frequency are 3
*/
task.sumHighestOccurrence(arr, n);
}
main();
Output
Result : 18
# Python 3 program for
# Find the sum of all highest occurring elements in an Array
class Occurrence :
def sumHighestOccurrence(self, arr, n) :
sum = 0
max = 1
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
if (record.get(arr[i]) > max) :
# Get most occuring element frequency
max = record.get(arr[i])
else :
# Add new element
record[arr[i]] = 1
i += 1
for key, value in record.items() :
if (value == max) :
# Add all occurrences
sum += (key * max)
print(" Result : ", sum)
def main() :
task = Occurrence()
arr = [8, 6, -3, 8, 3, 1, 5, 1, 1, -2, -3, -3, 8]
n = len(arr)
# [8 + 8 + 8] = 24
# [(-3) + (-3) + (-3)] = -9
# [1+1+1] = 3
# ------------------------------
# Result 18
# -----------------------------
# Note most occuring element frequency are 3
task.sumHighestOccurrence(arr, n)
if __name__ == "__main__": main()
Output
Result : 18
# Ruby program for
# Find the sum of all highest occurring elements in an Array
class Occurrence
def sumHighestOccurrence(arr, n)
sum = 0
max = 1
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
if (record[arr[i]] > max)
# Get most occuring element frequency
max = record[arr[i]]
end
else
# Add new element
record[arr[i]] = 1
end
i += 1
end
record.each { | key, value |
if (value == max)
# Add all occurrences
sum += (key * max)
end
}
print(" Result : ", sum, "\n")
end
end
def main()
task = Occurrence.new()
arr = [8, 6, -3, 8, 3, 1, 5, 1, 1, -2, -3, -3, 8]
n = arr.length
# [8 + 8 + 8] = 24
# [(-3) + (-3) + (-3)] = -9
# [1+1+1] = 3
# ------------------------------
# Result 18
# -----------------------------
# Note most occuring element frequency are 3
task.sumHighestOccurrence(arr, n)
end
main()
Output
Result : 18
import scala.collection.mutable._;
/*
Scala program for
Find the sum of all highest occurring elements in an Array
*/
class Occurrence()
{
def sumHighestOccurrence(arr: Array[Int], n: Int): Unit = {
var sum: Int = 0;
var max: Int = 1;
var record = 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);
if (record.get(arr(i)).get > max)
{
// Get most occuring element frequency
max = record.get(arr(i)).get;
}
}
else
{
// Add new element
record.addOne(arr(i), 1);
}
i += 1;
}
for ((key, value) <- record)
{
if (value == max)
{
// Add all occurrences
sum += (key * max);
}
}
//
println(" Result : " + sum);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Occurrence = new Occurrence();
var arr: Array[Int] = Array(
8, 6, -3, 8, 3, 1, 5, 1, 1, -2, -3, -3, 8
);
var n: Int = arr.length;
/*
[8 + 8 + 8] = 24
[(-3) + (-3) + (-3)] = -9
[1+1+1] = 3
------------------------------
Result 18
-----------------------------
Note most occuring element frequency are 3
*/
task.sumHighestOccurrence(arr, n);
}
}
Output
Result : 18
import Foundation;
/*
Swift 4 program for
Find the sum of all highest occurring elements in an Array
*/
class Occurrence
{
func sumHighestOccurrence(_ arr: [Int], _ n: Int)
{
var sum: Int = 0;
var max: Int = 1;
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;
if (record[arr[i]]! > max)
{
// Get most occuring element frequency
max = record[arr[i]]!;
}
}
else
{
// Add new element
record[arr[i]] = 1;
}
i += 1;
}
for (key, value) in record
{
if (value == max)
{
// Add all occurrences
sum += (key * max);
}
}
//
print(" Result : ", sum);
}
}
func main()
{
let task: Occurrence = Occurrence();
let arr: [Int] = [8, 6, -3, 8, 3, 1, 5, 1, 1, -2, -3, -3, 8];
let n: Int = arr.count;
/*
[8 + 8 + 8] = 24
[(-3) + (-3) + (-3)] = -9
[1+1+1] = 3
------------------------------
Result 18
-----------------------------
Note most occuring element frequency are 3
*/
task.sumHighestOccurrence(arr, n);
}
main();
Output
Result : 18
/*
Kotlin program for
Find the sum of all highest occurring elements in an Array
*/
class Occurrence
{
fun sumHighestOccurrence(arr: Array < Int > , n: Int): Unit
{
var sum: Int = 0;
var max: Int = 1;
val record: HashMap < Int, Int > = 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);
if (record.getValue(arr[i]) > max)
{
// Get most occuring element frequency
max = record.getValue(arr[i]);
}
}
else
{
// Add new element
record.put(arr[i], 1);
}
i += 1;
}
for ((key, value) in record)
{
if (value == max)
{
// Add all occurrences
sum += (key * max);
}
}
println(" Result : " + sum);
}
}
fun main(args: Array < String > ): Unit
{
val task: Occurrence = Occurrence();
val arr: Array < Int > = arrayOf(
8, 6, -3, 8, 3, 1, 5, 1, 1, -2, -3, -3, 8
);
val n: Int = arr.count();
/*
[8 + 8 + 8] = 24
[(-3) + (-3) + (-3)] = -9
[1+1+1] = 3
------------------------------
Result 18
-----------------------------
Note most occuring element frequency are 3
*/
task.sumHighestOccurrence(arr, n);
}
Output
Result : 18
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