New Want to Write for Computaholics ? if Yes Contact us at "contact@computaholics.in"

Find the Number Occurring Odd Number of Times

Find the Number Occurring Odd Number of Times
Given an array of positive integers. All numbers occur even number of times except one number which occurs odd number of times. Find the number in O(n) time & constant space.

#include <stdio.h>

int oddoccurrence(int ar[], int ar_size)
{
     int i;
     int result = 0;
     for (i=0; i < ar_size; i++)  
        result = result ^ ar[i];
   
     return result;
}

int main()
{
     int ar[] = {1, 2, 5, 4, 5, 1, 4, 2, 5, 1, 4, 4, 1};
     int n = sizeof(ar)/sizeof(ar[0]);
     printf("%d", oddoccurrence(ar, n));
     return 0;
}
Copyright © C Program | Java | OpenGL Programming | Hadoop at Computaholics