Find all odd frequency elements from an Array
Here given code implementation process.
// Java Program
// Find all odd frequency elements from an Array
import java.util.HashMap;
public class Frequency
{
public void OddOccurrence(int[] arr, int n)
{
// Use to count frequency
HashMap < Integer, Integer > record = new HashMap < Integer, Integer > ();
boolean result = false;
int i = 0;
System.out.print("\nArray Elements\n");
// Display array element
for ( i = 0; i < n ; i++ )
{
System.out.print(" "+arr[i]);
}
System.out.print("\n Result :");
// Execute loop through by array size n
for ( 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);
}
}
// Finding odd occurrence element and sum of its elements
for (int key: record.keySet())
{
if ((record.get(key) % 2) != 0)
{
System.out.print(" "+key);
result = true;
}
}
if(result==false)
{
System.out.print(" None \n");
}
}
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.OddOccurrence(arr, n);
}
}
Output
Array Elements
4 6 2 8 1 -2 4 2 1 1
Result : 1 -2 6 8
// Include header file
#include <iostream>
#include <unordered_map>
using namespace std;
// C++ Program
// Find all odd frequency elements from an Array
class Frequency
{
public: void OddOccurrence(int arr[], int n)
{
// Use to count frequency
unordered_map < int, int > record ;
bool result = false;
int i = 0;
cout << "\nArray Elements\n";
// Display array element
for (i = 0; i < n; i++)
{
cout << " " << arr[i];
}
cout << "\n Result :";
// Execute loop through by array size n
for (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;
}
}
for (auto &info: record)
{
if ((info.second % 2) != 0)
{
cout << " " << info.first;
result = true;
}
}
if (result == false)
{
cout << " None \n";
}
}
};
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.OddOccurrence(arr, n);
return 0;
}
Output
Array Elements
4 6 2 8 1 -2 4 2 1 1
Result : -2 1 6 8
// Include namespace system
using System;
using System.Collections.Generic;
// C# Program
// Find all odd frequency elements from an Array
public class Frequency
{
public void OddOccurrence(int[] arr, int n)
{
// Use to count frequency
Dictionary < int, int > record = new Dictionary < int, int > ();
// result indicator
Boolean result = false;
int i = 0;
Console.Write("\nArray Elements\n");
// Display array element
for (i = 0; i < n; i++)
{
Console.Write(" " + arr[i]);
}
Console.Write("\n Result :");
// Execute loop through by array size n
for (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);
}
}
foreach(KeyValuePair < int, int > info in record)
{
if ((info.Value % 2) != 0)
{
Console.Write(" " + info.Key);
result = true;
}
}
if (result == false)
{
Console.Write(" None \n");
}
}
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.OddOccurrence(arr, n);
}
}
Output
Array Elements
4 6 2 8 1 -2 4 2 1 1
Result : 6 8 1 -2
<?php
// Php Program
// Find all odd frequency elements from an Array
class Frequency
{
public function OddOccurrence( & $arr, $n)
{
// Use to count frequency
$record = array();
// result indicator
$result = false;
$i = 0;
echo "\nArray Elements\n";
// Display array element
for ($i = 0; $i < $n; $i++)
{
echo " ".$arr[$i];
}
echo "\n Result :";
// 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;
}
}
foreach($record as $key => $value)
{
if (($value % 2) != 0)
{
echo " ".$key;
$result = true;
}
}
if ($result == false)
{
echo " None \n";
}
}
}
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->OddOccurrence($arr, $n);
}
main();
Output
Array Elements
4 6 2 8 1 -2 4 2 1 1
Result : 6 8 1 -2
// Node Js Program
// Find all odd frequency elements from an Array
class Frequency
{
OddOccurrence(arr, n)
{
// Use to count frequency
var record = new Map();
// result indicator
var result = false;
var i = 0;
process.stdout.write("\nArray Elements\n");
// Display array element
for (i = 0; i < n; i++)
{
process.stdout.write(" " + arr[i]);
}
process.stdout.write("\n Result :");
// Execute loop through by array size n
for (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);
}
}
for (let [key, value] of record)
{
if ((record.get(key) % 2) != 0)
{
process.stdout.write(" " + key);
result = true;
}
}
if (result == false)
{
process.stdout.write(" None \n");
}
}
}
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.OddOccurrence(arr, n);
}
main();
Output
Array Elements
4 6 2 8 1 -2 4 2 1 1
Result : 6 8 1 -2
# Python 3 Program
# Find all odd frequency elements from an Array
class Frequency :
def OddOccurrence(self, arr, n) :
# Use to count frequency
record = dict()
# result indicator
result = False
i = 0
print("\nArray Elements")
# Display list element
while (i < n) :
print(" ", arr[i], end = "")
i += 1
print("\n Result :", end = "")
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
for key, value in record.items() :
if ((value % 2) != 0) :
print(" ", key, end = "")
result = True
if (result == False) :
print(" None ")
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.OddOccurrence(arr, n)
if __name__ == "__main__": main()
Output
Array Elements
4 6 2 8 1 -2 4 2 1 1
Result : 1 6 8 -2
# Ruby Program
# Find all odd frequency elements from an Array
class Frequency
def OddOccurrence(arr, n)
# Use to count frequency
record = Hash.new
# result indicator
result = false
i = 0
print("\nArray Elements\n")
# Display array element
while (i < n)
print(" ", arr[i])
i += 1
end
print("\n Result :")
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
record.each { | key, value |
if ((value % 2) != 0)
print(" ", key)
result = true
end
}
if (result == false)
print(" None \n")
end
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.OddOccurrence(arr, n)
end
main()
Output
Array Elements
4 6 2 8 1 -2 4 2 1 1
Result : 6 8 1 -2
import scala.collection.mutable._;
// Scala Program
// Find all odd frequency elements from an Array
class Frequency
{
def OddOccurrence(arr: Array[Int], n: Int): Unit = {
// Use to count frequency
var record = Map[Int, Int]();
// result indicator
var result: Boolean = false;
var i: Int = 0;
print("\nArray Elements\n");
// Display array element
while (i < n)
{
print(" " + arr(i));
i += 1;
}
print("\n Result :");
i = 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;
}
for ((key, value) <- record)
{
if ((value % 2) != 0)
{
print(" " + key);
result = true;
}
}
if (result == false)
{
print(" None \n");
}
}
}
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.OddOccurrence(arr, n);
}
}
Output
Array Elements
4 6 2 8 1 -2 4 2 1 1
Result : -2 1 6 8
import Foundation
// Swift 4 Program
// Find all odd frequency elements from an Array
class Frequency
{
func OddOccurrence(_ arr: [Int], _ n: Int)
{
// Use to count frequency
var record = [Int: Int]();
// result indicator
var result: Bool = false;
var i: Int = 0;
print("\nArray Elements");
// Display array element
while (i < n)
{
print(" ", arr[i], terminator: "");
i += 1;
}
print("\n Result :", terminator: "");
i = 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;
}
for (key, value) in record
{
if ((value % 2) != 0)
{
print(" ", key, terminator: "");
result = true;
}
}
if (result == false)
{
print(" None ");
}
}
}
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.OddOccurrence(arr, n);
}
main();
Output
Array Elements
4 6 2 8 1 -2 4 2 1 1
Result : 6 -2 8 1
// Kotlin Program
// Find all odd frequency elements from an Array
class Frequency
{
fun OddOccurrence(arr: Array < Int > , n: Int): Unit
{
// Use to count frequency
var record = mutableMapOf < Int , Int > ();
// result indicator
var result: Boolean = false;
var i: Int = 0;
print("\nArray Elements\n");
// Display array element
while (i < n)
{
print(" " + arr[i]);
i += 1;
}
print("\n Result :");
i = 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;
}
// Finding odd occurrence element and sum of its elements
for (key in record.keys)
{
if ((record.getValue(key) % 2) != 0)
{
print(" " + key);
result = true;
}
}
if (result == false)
{
print(" None \n");
}
}
}
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.OddOccurrence(arr, n);
}
Output
Array Elements
4 6 2 8 1 -2 4 2 1 1
Result : 6 8 1 -2
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