C Program to Find Two Odd Occurring Elements in an Array

This is a C Program to find the odd element given an array with only two different elements.

Problem Description

This C Program finds odd element in a given array with only two different element.

Problem Solution

Print the odd element with only two different elements in an array.

Program/Source Code

Here is source code of the C Program to find the odd element given an array with only two different element. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/*
 * C Program to Find the Odd Element given an Array with only two Different Element
 */
#include <stdio.h>
 
void printodd(int array[], int size)
{
    int xor2 = array[0]; /* Will hold XOR of two odd occurring elements */
    int set;
    int i;
    int n = size - 2;
    int x = 0, y = 0;
 
    /* The xor will basically be xor of two odd occurring elements */
    for (i = 1; i < size; i++)
        xor2 = xor2 ^ array[i]; 
    /* Get one set rightmost bit in the xor2. */
    set = xor2 & ~(xor2 - 1);
    /* Now divide elements in two sets: */
    for (i = 0; i < size; i++)
    {
        /* XOR of first set is finally going to hold one odd occurring number x */
        if (array[i] & set)
        x = x ^ array[i];
        /* XOR of second set is finally going to hold the other odd occurring number y */
        else
        y = y ^ array[i];
    }
    printf("\n The ODD elements are %d & %d ", x, y);
}
 
int main()
{
    int array[] = {10, 3, 2, 10, 2, 8, 8, 7};
    int arr_size = sizeof(array) / sizeof(array[0]);
    printodd(array, arr_size);
    getchar();
    return 0;
}
Program Explanation

In this C Program, we have defined the array[] variable elements. Using ‘array_size’ variable compute the division of size of previous array value by the next value of array[] variable element.

advertisement
advertisement

The printodd() function is used to find the odd element given an array with only two different elements. The xor2 variable is used to hold XOR of two odd occurring elements. For loop is used to compute, the xor will basically be xor of two odd occurring elements.

Divide the elements in two sets, XOR of first set is finally going to hold one odd occurring number x. The XOR of second set is finally going to hold the other odd occurring number y.

Runtime Test Cases
 
$ cc pgm87.c
$ a.out
 
 The ODD elements are 7 & 3

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Arrays, go to C Programming Examples on Arrays. If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.