Given an array of integers, every element appears twice except for one. Find that single one. Your algorithm should have a linear runtime complexity. Implement it without using extra memory.
The linear runtime complexity means we should have a O(n) algorithm, in other words, only one single loop (or multiple single loops). Double, nested loops are not allowed.
I guess the time constraint for this problem is 1 second because at first, my optimised
The O(n^2) algorithm
We need first to find out a unique number (integer) that can be used as a flag that indicates if a number has been checked before, so if yes, we can skip it. So, let’s find the minimal number of the list first,
int min = A[0];
for (int i = 1; i < n; i ++) {
if (A[i] < min) {
min = A[i];
}
}
And, we can start increment the number, if this has not been found the list, then we can use this number as a flag.
int kmin = min + 1;
do {
int flag = 0;
for (int i = 0; i < n; i ++) {
if (A[i] == kmin) {
flag = 1; // we cannot use this number
break;
}
}
if (flag == 0) break;
kmin ++;
} while (true);
Then a normal nested loop can be established obviously, checking current element (if not marked tested, because most numbers appear twice), return the number if it has only appeared once.
for (int i = 0; i < n; i ++) {
if (A[i] == kmin) continue; // has appeared before
int c = 0;
int k = A[i];
A[i] = kmin;
for (int j = i + 1; j < n; j ++) {
if (A[j] == k) {
c ++;
A[j] = kmin; // mark this so to skip next time
if (c == 1) break; // maximum twice
}
}
if (c != 1) return k;
}
This got accepted in 872ms, clearly not fast enough.
The O(n) algorithm
The
class Solution {
public:
int singleNumber(int A[], int n) {
while(--n > 0) { // to reuse the parameter, no additional storage
A[n - 1] ^= A[n];
}
return A[0];
}
};
Oh yeah! We have a AC in only 48ms! (20 times faster!)
Teaching Kids Programming – Find the Single Number in Array
–EOF (The Ultimate Computing & Technology Blog) —
Last Post: How to Implement a LRU (Least Recently Used) Cache in C++?
Next Post: Coding Exercise - Simple Stack Implementation in C++