Need help with : Find pairs with difference k in a array

Given an array of integers and a target value, determine the number of pairs of array elements that have a difference equal to the target value.

Question link Pairs | HackerRank

I have used a simple solution using a hash map

unordered_map<int, int> map;
int ans = 0;

for(auto val: arr) {
    map[val]++;
}

for(auto it: map) {
    int elem = it.first;
    
    if(map[elem + k] > 0) {
        // cout<<elem<<" "<<elem+k<<"\n";
        ans++;
    }
}

return ans;

but in SOME cases the answer comes double, can someone help me debug it, I tried for hours but couldn’t find any lead