Skip to main content

Find the numbers occurring odd number of times

Here given code implementation process.

// C program 
// Find the numbers occurring odd number of times 
#include <iostream>
#include <map>
using namespace std;

void oddOccurrences(int num[], int n)
{
    // Create a map
    map < int, int > mp;
    int result = 0;

    // Execute loop through by size
    for (int i = 0; i < n; ++i)
    {
        // Counting occurrences of array elements
        mp[num[i]]++;
    }
    cout << "\n Odd occurring element : " << endl;

    // iterating map elements
    for (auto it: mp)
    {
        if (it.second % 2 != 0)
        {
            cout << "  " << it.first;
            result++;
        }
    }
    if (result == 0)
    {
        cout << " None " << endl;
    }
}
int main(int argc, char
    const *argv[])
{
    int num[] = {
        1 , 8 , 2 , 8 , 6 , 2 , 11 , 1 , 11 , 2 , 1 , 3
    };
    int n = sizeof(num) / sizeof(num[0]);
    oddOccurrences(num, n);
    return 0;
}

Output

 Odd occurring element :
  1  2  3  6
/*
  Java program
  Find the numbers occurring odd number of times 
*/
import java.util.HashMap;
import java.util.Map;
public class Occurrence
{

    // Find all elements in given array which is occurs 
    // odd number of times
    public void oddOccurrences(int[] num, int n)
    {
       
        // Create a empty map
        Map < Integer, Integer > map = new HashMap <Integer,Integer> ();
        // Execute loop through by array size
        for (int i = 0; i < n; i++)
        {
            if (map.containsKey(num[i]))
            {
                // When key exists then update value
                map.put(num[i], map.get(num[i]) + 1);
            }
            else
            {
                map.put(num[i], 1);
            }
        }
        System.out.print(" Odd occurring element : \n");
        // iterating map elements
        for (int key: map.keySet())
        {
            if( (map.get(key) % 2) != 0)
            {
                System.out.print("  "+key );
            }
            
        }
    }
    public static void main(String[] args)
    {
        Occurrence task = new Occurrence();

        // Define array of integer elements
        int []num = 
        {
            1 , 8 , 2 , 8 , 6 , 2 , 11 , 1 , 11 , 2 , 1 , 3
        };
        // Get number of elements
        int n = num.length;

        task.oddOccurrences(num, n);
    }
}

Output

 Odd occurring element :
  1  2  3  6
// Include namespace system
using System;
using System.Collections.Generic;
public class Occurrence
{
	// Find all elements in given array which is occurs
	// odd number of times
	public void oddOccurrences(int[] num, int n)
	{
		// Create a empty map
		Dictionary < int, int > dict = new Dictionary < int, int > ();
		// Execute loop through by array size
		for (int i = 0; i < n; i++)
		{
			if (dict.ContainsKey(num[i]))
			{
				dict[num[i]] = dict[num[i]] + 1;
			}
			else
			{
				dict.Add(num[i], 1);
			}
		}
		Console.Write(" Odd occurring element : \n");
		foreach(KeyValuePair < int, int > entry in dict)
		{
			if ((dict[entry.Key] % 2) != 0)
			{
				Console.Write("  " + entry.Key);
			}
		}
	}
	public static void Main(String[] args)
	{
		Occurrence task = new Occurrence();
		// Define array of integer elements
		int[] num = {
			1 , 8 , 2 , 8 , 6 , 2 , 11 , 1 , 11 , 2 , 1 , 3
		};
		// Get number of elements
		int n = num.Length;
		task.oddOccurrences(num, n);
	}
}

Output

 Odd occurring element :
  1  2  6  3
<?php
/*
  Php program
  Find the numbers occurring odd number of times 
*/
class Occurrence
{
	// Find all elements in given array which is occurs
	// odd number of times
	public	function oddOccurrences($num, $n)
	{
		// Create a empty array
		$map = array();
		// Execute loop through by array size
		for ($i = 0; $i < $n; $i++)
		{
			if (array_key_exists($num[$i], $map))
			{
				$map[$num[$i]] = $map[$num[$i]] + 1;
			}
			else
			{
				$map[$num[$i]] = 1;
			}
		}
		echo " Odd occurring element : \n";
		foreach($map as $key => $value)
		{
			if (($value % 2) != 0)
			{
				echo "  ". $key;
			}
		}
	}
}

function main()
{
	$task = new Occurrence();
	// Define array of integer elements
	$num = array(1, 8, 2, 8, 6, 2, 11, 1, 11, 2, 1, 3);
	// Get number of elements
	$n = count($num);
	$task->oddOccurrences($num, $n);
}
main();

Output

 Odd occurring element :
  1  2  6  3
/*
  Node Js program
  Find the numbers occurring odd number of times 
*/
class Occurrence
{
	// Find all elements in given array which is occurs
	// odd number of times
	oddOccurrences(num, n)
	{
		// Create a empty map
		var map = new Map();
		// Execute loop through by array size
		for (var i = 0; i < n; i++)
		{
			if (map.has(num[i]))
			{
				map.set(num[i], map.get(num[i]) + 1);
			}
			else
			{
				map.set(num[i], 1);
			}
		}
		process.stdout.write(" Odd occurring element : \n");
		for (let [key, value] of map)
		{
			if ((value % 2) != 0)
			{
				process.stdout.write("  " + key);
			}
		}
	}
}

function main()
{
	var task = new Occurrence();
	// Define array of integer elements
	var num = [1, 8, 2, 8, 6, 2, 11, 1, 11, 2, 1, 3];
	// Get number of elements
	var n = num.length;
	task.oddOccurrences(num, n);
}
main();

Output

 Odd occurring element :
  1  2  6  3
#   Python 3 program
#   Find the numbers occurring odd number of times 

class Occurrence :
	#  Find all elements in given list which is occurs
	#  odd number of times
	def oddOccurrences(self, num, n) :
		#  Create a empty dict
		map = dict()
		#  Execute loop through by list size
		i = 0
		while (i < n) :
			if (num[i] in map.keys()) :
				map[num[i]] = map.get(num[i]) + 1
			else :
				map[num[i]] = 1
			
			i += 1
		
		print(" Odd occurring element : ")
		for key, value in map.items() :
			if ((value % 2) != 0) :
				print("  ", key, end = "")
			
		
	

def main() :
	task = Occurrence()
	#  Define list of integer elements
	num = [1, 8, 2, 8, 6, 2, 11, 1, 11, 2, 1, 3]
	#  Get number of elements
	n = len(num)
	task.oddOccurrences(num, n)

if __name__ == "__main__": main()

Output

 Odd occurring element :
   1   2   3   6
#   Ruby program
#   Find the numbers occurring odd number of times 

class Occurrence 
	#  Find all elements in given array which is occurs
	#  odd number of times
	def oddOccurrences(num, n) 
		#  Create a empty hash
		map = Hash.new 
		#  Execute loop through by array size
		i = 0
		while (i < n) 
			if (map.key?(num[i])) 
				map[num[i]] = map[num[i]] + 1
			else 
				map[num[i]] = 1
			end

			i += 1
		end

		print(" Odd occurring element : \n")
		map.each { | key, value | 
			if ((map[key] % 2) != 0) 
				print("  ", key)
			end

		}
	end

end

def main() 
	task = Occurrence.new()
	#  Define array of integer elements
	num = [1, 8, 2, 8, 6, 2, 11, 1, 11, 2, 1, 3]
	#  Get number of elements
	n = num.length
	task.oddOccurrences(num, n)
end

main()

Output

 Odd occurring element : 
  1  2  6  3
import scala.collection.mutable._;
/*
  Scala program
  Find the numbers occurring odd number of times 
*/
class Occurrence
{
	// Find all elements in given array which is occurs
	// odd number of times
	def oddOccurrences(num: Array[Int], n: Int): Unit = {
		// Create a empty map
		var map: Map[Int, Int] = Map();
		// Execute loop through by array size
		var i: Int = 0;
		while (i < n)
		{
			if (map.contains(num(i)))
			{
				map(num(i)) = map.get(num(i)).get + 1;
			}
			else
			{
				map += (num(i) -> 1);
			}
			i += 1;
		}
		print(" Odd occurring element : \n");
		map.keys.foreach
		{	key  => 
			if ((map.get(key).get % 2) != 0)
			{
				print("  " + key);
			}
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Occurrence = new Occurrence();
		// Define array of integer elements
		var num: Array[Int] = Array(1, 8, 2, 8, 6, 2, 11, 1, 11, 2, 1, 3);
		// Get number of elements
		var n: Int = num.length;
		task.oddOccurrences(num, n);
	}
}

Output

 Odd occurring element :
  1  2  3  6
/*
  Swift 4 program
  Find the numbers occurring odd number of times 
*/
class Occurrence
{
	// Find all elements in given array which is occurs
	// odd number of times
	func oddOccurrences(_ num: [Int], _ n: Int)
	{
		// Create a empty dict
		var map = [Int: Int]();
		
		var i: Int = 0;
      	// Execute loop through by array size
		while (i < n)
		{
			if (map.keys.contains(num[i]))
			{
				map[num[i]] = map[num[i]]! + 1;
			}
			else
			{
				map[num[i]] = 1;
			}
			i += 1;
		}
		print(" Odd occurring element : ");
		for (key, value) in map
		{
			if ((value % 2)  != 0)
			{
				print("  ", key, terminator: "");
			}
		}
	}
}
func main()
{
	let task: Occurrence = Occurrence();
	// Define array of integer elements
	let num: [Int] = [1, 8, 2, 8, 6, 2, 11, 1, 11, 2, 1, 3];
	// Get number of elements
	let n: Int = num.count;
	task.oddOccurrences(num, n);
}
main();

Output

 Odd occurring element :
   6   2   3   1
/*
  Kotlin program
  Find the numbers occurring odd number of times 
*/
class Occurrence
{
	// Find all elements in given array which is occurs
	// odd number of times
	fun oddOccurrences(num: Array < Int > , n: Int): Unit
	{
		// Create a empty map

		var map = mutableMapOf<Int, Int>();
		var i: Int = 0;
		// Execute loop through by array size
		while (i < n)
		{
			if (map.containsKey(num[i]))
			{
				map.put(num[i], map.getValue(num[i]) + 1);
			}
			else
			{
				map.put(num[i], 1);
			}
			i += 1;
		}
		print(" Odd occurring element : \n");
		// iterating map elements
		for (key in map.keys)
		{
			if ((map.getValue(key) % 2) != 0)
			{
				print("  " + key);
			}
		}
	}
}
fun main(args: Array <String> ): Unit
{
	var task: Occurrence = Occurrence();
	// Define array of integer elements
	var num: Array <Int> = arrayOf(1, 8, 2, 8, 6, 2, 11, 1, 11, 2, 1, 3);
	// Get number of elements
	var n: Int = num.count();
	task.oddOccurrences(num, n);
}

Output

 Odd occurring element :
  1  2  6  3




Comment

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