Problem Statement
There are N integers in an array A. All but one integer occur in pairs. Your task is to find out the number that occurs only once.
Input Format
The first line of the input contains an integer N indicating the number of integers.
The next line contains N space separated integers that form the array A.
Constraints
1 <= N < 100
N % 2 = 1 ( N is an odd number )
0 <= A[i] <= 100, ∀ i ∈ [1, N]
Output Format
Output S, the number that occurs only once.
Sample Input:1
1
1
Sample Output:1
1
Sample Input:2
3
1 1 2
Sample Output:2
2
Sample Input:3
5
0 0 1 2 1
Sample Output:3
2
Explanation
In the first input, we see only one element (1) and that element is the answer.
In the second input, we see three elements, 1 exists at two places. The element that occurs only once is 2.
In the third input, we see five elements. 1 and 0 exist at two places. The element that occurs only once is 2.
Copyright (c) 2015 HackerRank.
All Rights Reserved
Suggest Edits
28199 hackers have submitted code
#include
#include
#include
#include
#include
int lonelyinteger(int a_size, int* a) {
int sum=0,i,j;
if(a_size %2 == 0)
return 0;
for(i=0;i<a_size;i++)
sum +=a[i];
for(i=0;i<a_size;i++)
{
if((sum-a[i])%2==0 && a[i] !=0)
{
for(j=i+1;j<a_size;j++)
{
if(a[i]==a[j])
break;
}
if(j==a_size)
return a[i];
}
}
return 0;
}
int main() {
int res;
int _a_size, _a_i;
scanf("%d", &_a_size);
int _a[_a_size];
for(_a_i = 0; _a_i < _a_size; _a_i++) {
int _a_item;
scanf("%d", &_a_item);
_a[_a_i] = _a_item;
}
res = lonelyinteger(_a_size, _a);
printf("%d", res);
return 0;
}
Compiled & Run : http://ideone.com/gLHC74