Given an array of positive integers. Each number in the array repeats even number of times, only 1 number repeats odd number of times. Find that number.
This Solution is O(n) Time complexity.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[]={4,3,4,3,1};
int len=(sizeof(a)/sizeof(int)),n=a[0],i;
clrscr();
for(i=1;i<len;i++)
{
n^=a[i];
}
printf("%d\n",n);
getch();
}
This Solution is O(n) Time complexity.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[]={4,3,4,3,1};
int len=(sizeof(a)/sizeof(int)),n=a[0],i;
clrscr();
for(i=1;i<len;i++)
{
n^=a[i];
}
printf("%d\n",n);
getch();
}
No comments:
Post a Comment