This solution is based on some knowledge of Digital Logic Design.

```
class Solution {
public:
int singleNumber(int A[], int n) {
int curVal = 0;
int curState = 0;
for(int i=0; i<n; i++)
{
int temp1 = curVal & A[i] & (~curState);
int temp2 = (~A[i]) & curState;
curVal = (curVal^A[i])&(~curState);
curState = temp1|temp2;
}
return curVal;
}
};
```