forked from haoel/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingleNumber.cpp
More file actions
30 lines (26 loc) · 748 Bytes
/
singleNumber.cpp
File metadata and controls
30 lines (26 loc) · 748 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// Source : https://oj.leetcode.com/problems/single-number/
// Author : Hao Chen
// Date : 2014-06-17
/**********************************************************************************
*
* Given an array of integers, every element appears twice except for one. Find that single one.
*
* Note:
* Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
*
*
**********************************************************************************/
#include <stdio.h>
int singleNumber(int A[], int n) {
int s = 0;
for(int i=0; i<n; i++){
s = s^A[i];
}
return s;
}
int main()
{
int a[]={1,1,2,2,3};
printf("%d\n", singleNumber(a,5));
return 0;
}