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.
Example:
I/P = [1, 2, 3, 2, 3, 1, 3]
O/P = 3
Algorithm:
Do bitwise XOR of all the elements. Finally we get the number which has odd occurrences.
#include <stdio.h> int getOddOccurrence( int ar[], int ar_size) { int i; int res = 0; for (i=0; i < ar_size; i++) res = res ^ ar[i]; return res; } |
Read full article from Find the Number Occurring Odd Number of Times | GeeksforGeeks
No comments:
Post a Comment