Easy HashMap based approach in C++


#1
int Solution::solve(vector<int> &A, int B)
{
    //Important property:- If x^y=z then y^z=x and x^z=y
    unordered_map<int,int>m;
    for(int i=0;i<A.size();i++)
    {
        m[A[i]]=i;
    }
    int count=0;
    for(int i=0;i<A.size();i++)
    {
        // Try to find a element( say x) such that A[i]^x=B and in order to
        // avoid duplicacy in counting make sure that the index of the searched
        // value is greater than the index of the current value.
        if(m.find(B^A[i])!=m.end() && m[B^A[i]]>i)
        {
            count++;
        }
    }
    return count;
}