C Program to find the Element with an odd number of count in an Array in which all other elements are present even number of times - Bit Manipulation
// Program by Akash Tripathi (@proakash256)
// In an Integer Array ,
// all the Numbers are present
// Even number of times except one.
// Find that Integer with Odd count in
// O(n) time complexity and
// O(1) Space Complexity.
// 0 ^ n = n
// n ^ n = 0
// In this method , we do XOR(^) of
// each element with each other using a loop.
// Duplicate elements are eliminated by the above
// properties of XOR and hence
// we find the required element.
#include <stdio.h>
int main()
{
int n , result = 0;
printf("Enter the number of elements
you want to enter : ");
scanf("%d", &n);
int ar[n];
printf("\nEnter the Elements :\n");
for (int i = 0; i < n; i = i + 1)
scanf("%d", &ar[i]);
for (int i = 0; i < n; i = i + 1)
{
result = result ^ ar[i];
}
printf("\nThe Element which is present
Odd Number of times is %d." , result);
return 0;
}
Comments
Post a Comment