Open In App

C# Program to Find the Number Occurring Odd Number of Times

Last Updated : 24 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Write a C# program for a given array of positive integers. All numbers occur an even number of times except one number which occurs an odd number of times. Find the number in O(n) time & constant space.

Examples :

Input: arr = {1, 2, 3, 2, 3, 1, 3}
Output : 3

Input: arr = {5, 7, 2, 7, 5, 2, 5}
Output: 5

C# Program to Find the Number Occurring Odd Number of Times using Nested Loop:

A Simple Solution is to run two nested loops. The outer loop picks all elements one by one and the inner loop counts the number of occurrences of the element picked by the outer loop.

Below is the implementation of the brute force approach : 

C#




// C# program to find the element
// occurring odd number of times
using System;
 
class GFG
{
    // Function to find the element
    // occurring odd number of times
    static int getOddOccurrence(int []arr, int arr_size)
    {
        for (int i = 0; i < arr_size; i++) {
            int count = 0;
             
            for (int j = 0; j < arr_size; j++) {
                if (arr[i] == arr[j])
                    count++;
            }
            if (count % 2 != 0)
                return arr[i];
        }
        return -1;
    }
     
    // Driver code
    public static void Main()
    {
        int []arr = { 2, 3, 5, 4, 5, 2, 4, 3, 5, 2, 4, 4, 2 };
        int n = arr.Length;
        Console.Write(getOddOccurrence(arr, n));
    }
}
 
// This code is contributed by Sam007


Output

5

Time Complexity: O(n^2)
Auxiliary Space: O(1)

C# Program to Find the Number Occurring Odd Number of Times using Hashing:

A Better Solution is to use Hashing. Use array elements as a key and their counts as values. Create an empty hash table. One by one traverse the given array elements and store counts.

Below is the implementation of the above approach:

C#




// C# program to find the element occurring odd
// number of times
using System;
using System.Collections.Generic;
 
public class OddOccurrence
{
    // function to find the element occurring odd
    // number of times
    static int getOddOccurrence(int []arr, int n)
    {
        Dictionary<int,int> hmap = new Dictionary<int,int>();
         
        // Putting all elements into the HashMap
        for(int i = 0; i < n; i++)
        {
            if(hmap.ContainsKey(arr[i]))
            {
                int val = hmap[arr[i]];
                         
                // If array element is already present then
                // increase the count of that element.
                hmap.Remove(arr[i]);
                hmap.Add(arr[i], val + 1);
            }
            else
                 
                // if array element is not present then put
                // element into the HashMap and initialize
                // the count to one.
                hmap.Add(arr[i], 1);
        }
 
        // Checking for odd occurrence of each element present
        // in the HashMap
        foreach(KeyValuePair<int, int> entry in hmap)
        {
            if(entry.Value % 2 != 0)
            {
                return entry.Key;
            }
        }
        return -1;
    }
         
    // Driver code
    public static void Main(String[] args)
    {
        int []arr = new int[]{2, 3, 5, 4, 5, 2, 4, 3, 5, 2, 4, 4, 2};
        int n = arr.Length;
        Console.WriteLine(getOddOccurrence(arr, n));
    }
}
 
// This code is contributed by Princi Singh


Output

5

Time Complexity: O(n)
Auxiliary Space: O(n)

C# Program to Find the Number Occurring Odd Number of Times using Bit Manipulation:

The Best Solution is to do bitwise XOR of all the elements. XOR of all elements gives us odd occurring elements. 

Here ^ is the XOR operators;Note :x^0 = xx^y=y^x (Commutative property holds)(x^y)^z = x^(y^z) (Distributive property holds)x^x=0

Below is the implementation of the above approach.  

C#




// C# program to find the element
// occurring odd number of times
using System;
 
class GFG
{
    // Function to find the element
    // occurring odd number of times
    static int getOddOccurrence(int []arr, int arr_size)
    {
        int res = 0;
        for (int i = 0; i < arr_size; i++)
        {
            res = res ^ arr[i];
        }
        return res;
    }
     
    // Driver code
    public static void Main()
    {
        int []arr = { 2, 3, 5, 4, 5, 2, 4, 3, 5, 2, 4, 4, 2 };
        int n = arr.Length;
        Console.Write(getOddOccurrence(arr, n));
    }
}
 
// This code is contributed by Sam007


Output

5

Time Complexity: O(n)
Auxiliary Space: O(1)

Please refer complete article on Find the Number Occurring Odd Number of Times for more details!



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads