Sum of all odd frequency elements in an array

Here given code implementation process.

// Java Program 
// Sum of all odd frequency elements in an array
import java.util.HashMap;
public class Frequency
{
    public void sumOddOccurrence(int[] arr, int n)
    {
        // Use to count frequency
        HashMap < Integer, Integer > record = new HashMap < Integer, Integer > ();
        // Execute loop through by array size n
        for (int i = 0; i < n; i++)
        {
            if (record.containsKey(arr[i]))
            {
                // increase element frequency
                record.put(arr[i], record.get(arr[i]) + 1);
            }
            else
            {
                // Add new element
                record.put(arr[i], 1);
            }
        }

        int sum = 0;

        // Finding odd occurrence element and sum of its elements
        for (int key: record.keySet())
        {
            if ((record.get(key) % 2) != 0)
            {
                // Sum element by odd Occurrence
                sum += key * record.get(key);
            }
        }
        // Display calculated result
        System.out.print(" Result : " + sum);
    }
    public static void main(String[] args)
    {
        Frequency task = new Frequency();

        int []arr = 
        {
            4 , 6 , 2 , 8 , 1 , -2 , 4 , 2 , 1 , 1
        };
        // Get the size of array
        int n = arr.length;
        // Test
        task.sumOddOccurrence(arr, n);
    }
}

Output

 Result : 15
// Include header file
#include <iostream>
#include <unordered_map>
using namespace std;

// C++ Program
// Sum of all odd frequency elements in an array

class Frequency
{
	public: void sumOddOccurrence(int arr[], int n)
	{
		// Use to count frequency
		unordered_map < int, int > record ;
		// Execute loop through by array size n
		for (int i = 0; i < n; i++)
		{
			if (record.find(arr[i]) != record.end())
			{
				// increase element frequency
				record[arr[i]] = record[arr[i]] + 1;
			}
			else
			{
				// Add new element
				record[arr[i]] = 1;
			}
		}
		int sum = 0;
		for (auto &info: record)
		{
			if ((info.second % 2) != 0)
			{
				// Sum element by odd Occurrence
				sum += info.first * info.second;
			}
		}
		cout << " Result : " << sum;
	}
};
int main()
{
	Frequency task = Frequency();
	int arr[] = {
		4 , 6 , 2 , 8 , 1 , -2 , 4 , 2 , 1 , 1
	};
	// Get the size of array
	int n = sizeof(arr) / sizeof(arr[0]);
	// Test
	task.sumOddOccurrence(arr, n);
	return 0;
}

Output

 Result : 15
// Include namespace system
using System;
using System.Collections.Generic;
// C# Program
// Sum of all odd frequency elements in an array
public class Frequency
{
	public void sumOddOccurrence(int[] arr, int n)
	{
		// Use to count frequency
		Dictionary < int, int > record = new Dictionary < int, int > ();
		// Execute loop through by array size n
		for (int i = 0; i < n; i++)
		{
			if (record.ContainsKey(arr[i]))
			{
				// increase element frequency
				record[arr[i]] = record[arr[i]] + 1;
			}
			else
			{
				// Add new element
				record.Add(arr[i], 1);
			}
		}
		int sum = 0;
		foreach(KeyValuePair < int, int > info in record)
		{
			if ((info.Value % 2) != 0)
			{
				// Sum element by odd Occurrence
				sum += info.Key * info.Value;
			}
		}
		// Display calculated result
		Console.Write(" Result : " + sum);
	}
	public static void Main(String[] args)
	{
		Frequency task = new Frequency();
		int[] arr = {
			4 , 6 , 2 , 8 , 1 , -2 , 4 , 2 , 1 , 1
		};
		// Get the size of array
		int n = arr.Length;
		// Test
		task.sumOddOccurrence(arr, n);
	}
}

Output

 Result : 15
<?php
// Php Program
// Sum of all odd frequency elements in an array
class Frequency
{
	public	function sumOddOccurrence( & $arr, $n)
	{
		// Use to count frequency
		$record = array();
		// Execute loop through by array size n
		for ($i = 0; $i < $n; $i++)
		{
			if (array_key_exists($arr[$i], $record))
			{ // increase element frequency
				$record[$arr[$i]] = $record[$arr[$i]] + 1;
			}
			else
			{ // Add new element
				$record[$arr[$i]] = 1;
			}
		}
		$sum = 0;
		foreach($record as $key => $value)
		{
			if (($value % 2) != 0)
			{
				// Sum element by odd Occurrence
				$sum += $key * $value;
			}
		}
		// Display calculated result
		echo " Result : ".$sum;
	}
}

function main()
{
	$task = new Frequency();
	$arr = array(4, 6, 2, 8, 1, -2, 4, 2, 1, 1);
	// Get the size of array
	$n = count($arr); 
       // Test
	$task->sumOddOccurrence($arr, $n);
}
main();

Output

 Result : 15
// Node Js Program
// Sum of all odd frequency elements in an array
class Frequency
{
	sumOddOccurrence(arr, n)
	{
		// Use to count frequency
		var record = new Map();
		// Execute loop through by array size n
		for (var i = 0; i < n; i++)
		{
			if (record.has(arr[i]))
			{
				// increase element frequency
				record.set(arr[i], record.get(arr[i]) + 1);
			}
			else
			{
				// Add new element
				record.set(arr[i], 1);
			}
		}
		var sum = 0;
		for (let [key, value] of record)
		{
			if ((value % 2) != 0)
			{
				// Sum element by odd Occurrence
				sum += key * value;
			}
		}
		// Display calculated result
		process.stdout.write(" Result : " + sum);
	}
}

function main()
{
	var task = new Frequency();
	var arr = [4, 6, 2, 8, 1, -2, 4, 2, 1, 1];
	// Get the size of array
	var n = arr.length;
	// Test
	task.sumOddOccurrence(arr, n);
}
main();

Output

 Result : 15
#  Python 3 Program 
#  Sum of all odd frequency elements in an array
class Frequency :
	def sumOddOccurrence(self, arr, n) :
		#  Use to count frequency
		record = dict()
		i = 0
		#  Execute loop through by list size n
		while (i < n) :
			if (arr[i] in record.keys()) :
				#  increase element frequency
				record[arr[i]] = record.get(arr[i]) + 1
			else :
				#  Add new element
				record[arr[i]] = 1
			
			i += 1
		
		sum = 0
		for key, value in record.items() :
			if ((value % 2) != 0) :
				#  Sum element by odd Occurrence
				sum += key * value
			
		
		#  Display calculated result
		print(" Result : ", sum, end = "")
	

def main() :
	task = Frequency()
	arr = [4, 6, 2, 8, 1, -2, 4, 2, 1, 1]
	#  Get the size of list
	n = len(arr) 
	#  Test
	task.sumOddOccurrence(arr, n)

if __name__ == "__main__": main()

Output

 Result :  15
#  Ruby Program 
#  Sum of all odd frequency elements in an array
class Frequency 
	def sumOddOccurrence(arr, n) 
		#  Use to count frequency
		record = Hash.new 
		i = 0
		#  Execute loop through by array size n
		while (i < n) 
			if (record.key?(arr[i])) 
				record[arr[i]] = record[arr[i]] + 1
			else 
				record[arr[i]] = 1
			end
			i += 1
		end

		sum = 0
		record.each { | key, value | 
			if ((value % 2) != 0) 
				#  Sum element by odd Occurrence
				sum += key * value
			end
		}
		#  Display calculated result
		print(" Result : ", sum)
	end

end

def main() 
	task = Frequency.new()
	arr = [4, 6, 2, 8, 1, -2, 4, 2, 1, 1]
	#  Get the size of array
	n = arr.length
	#  Test
	task.sumOddOccurrence(arr, n)
end

main()

Output

 Result : 15
import scala.collection.mutable._;
// Scala Program
// Sum of all odd frequency elements in an array
class Frequency
{
	def sumOddOccurrence(arr: Array[Int], n: Int): Unit = {
		// Use to count frequency
		var record = Map[Int, Int]();
		var i: Int = 0;
		// Execute loop through by array size n
		while (i < n)
		{
			if (record.contains(arr(i)))
			{
				// increase element frequency
				record.addOne(arr(i), record.get(arr(i)).get + 1);
			}
			else
			{
				// Add new element
				record.addOne(arr(i), 1);
			}
			i += 1;
		}
		var sum: Int = 0;
		for ((key, value) <- record)
		{
			if ((value % 2) != 0)
			{
				// Sum element by odd Occurrence
				sum += key * value;
			}
		}
		// Display calculated result
		print(" Result : " + sum);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Frequency = new Frequency();
		var arr: Array[Int] = Array(4, 6, 2, 8, 1, -2, 4, 2, 1, 1);
		// Get the size of array
		var n: Int = arr.length;
		// Test
		task.sumOddOccurrence(arr, n);
	}
}

Output

 Result : 15
import Foundation
// Swift 4 Program
// Sum of all odd frequency elements in an array
class Frequency
{
	func sumOddOccurrence(_ arr: [Int], _ n: Int)
	{
		// Use to count frequency
		var record = [Int: Int]();
		var i: Int = 0;
		// Execute loop through by array size n
		while (i < n)
		{
			if (record.keys.contains(arr[i]))
			{
				// increase element frequency
				record[arr[i]] = record[arr[i]]! + 1;
			}
			else
			{
				// Add new element
				record[arr[i]] = 1;
			}
			i += 1;
		}
		var sum: Int = 0;
		for (key, value) in record
		{
			if ((value % 2)  != 0)
			{
				// Sum element by odd Occurrence
				sum += key * value;
			}
		}
		// Display calculated result
		print(" Result : ", sum, terminator: "");
	}
}
func main()
{
	let task: Frequency = Frequency();
	let arr: [Int] = [4, 6, 2, 8, 1, -2, 4, 2, 1, 1];
	// Get the size of array
	let n: Int = arr.count;
	// Test
	task.sumOddOccurrence(arr, n);
}
main();

Output

 Result :  15
// Kotlin Program
// Sum of all odd frequency elements in an array
class Frequency
{
	fun sumOddOccurrence(arr: Array < Int > , n: Int): Unit
	{
		// Use to count frequency
		var record = mutableMapOf < Int , Int > ();
		var i: Int = 0;
		// Execute loop through by array size n
		while (i < n)
		{
			if (record.containsKey(arr[i]))
			{
				// increase element frequency
				record.put(arr[i], record.getValue(arr[i]) + 1);
			}
			else
			{
				// Add new element
				record.put(arr[i], 1);
			}
			i += 1;
		}
		var sum: Int = 0;
		// Finding odd occurrence element and sum of its elements
		for (key in record.keys)
		{
			if ((record.getValue(key) % 2) != 0)
			{
				// Sum element by odd Occurrence
				sum += key * record.getValue(key);
			}
		}
		// Display calculated result
		print(" Result : " + sum);
	}
}
fun main(args: Array < String > ): Unit
{
	var task: Frequency = Frequency();
	var arr: Array < Int > = arrayOf(4, 6, 2, 8, 1, -2, 4, 2, 1, 1);
	// Get the size of array
	var n: Int = arr.count();
	// Test
	task.sumOddOccurrence(arr, n);
}

Output

 Result : 15


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







© 2021, kalkicode.com, All rights reserved