#include <stdio.h>
#include <stdlib.h>
int specialNo(int a[], int n)
{
int x = 0;
for (int i = 0; i < n; i++)
{
// xoring all elements outputs the special number
x ^= a[i];
}
// return the special number
return x;
}
int main()
{
int x, count = 0;
// array of positive no initialized to -1 for now
int a[20] = {-1};
printf("enter positive numbers ending with -1\n");
scanf("%d", &x);
// first no
a[0] = x;
while (x != -1)
{
scanf("%d", &x);
// from second no onwards
a[count + 1] = x;
count++;
}
// function to check duplicate exists or not
printf("\nthe special number occuring odd no of times is %d\n", specialNo(a, count));
return 0;
}
Output
enter positive numbers ending with -1
5
4
3
5
9
4
7
-1
the special number occuring odd no of times is 13
0 Comments