Easy | Commented | JavaScript hashmap | O(n) solution
\nvar findPairs = function (nums, k) {\n\tlet map = {}, //Object to store count/frequency of numbers in array\n\t\tcount = 0; //count the desired output/result\
```\nvar findPairs = function (nums, k) {\n\tlet map = {}, //Object to store count/frequency of numbers in array\n\t\tcount = 0; //count the desired output/result\n\n\t//loop through the array and store the count/frequency in the object\n\tfor (let i = 0; i < nums.length; i++) {\n\t\t/*if num appears for the 1st time them map[nums[i]] will be undefined\n\tand undefined||0 will result in 0 and 0+1 will store 1 as count of that number */\n\t\tmap[nums[i]] = (map[nums[i]] || 0) + 1;\n\t}\n\n\t//loop through keys.In this case keys will be unique as they have the frequency of their occurrences\n\tObject.keys(map).forEach((key) => {\n\t\t/* Now we need to check if target k is 0 or not \n\t\tbecause in case k is 0 then only possible combination to get \n\t\tdifference 0 will be when same num appear twice i.e \n\t\t1-1=0 ,here 1 count has to be 2 in map to get diff 0\n\t\t*/\n\t\tif (k !== 0) {\n\t\t\t/* \n\t\t\tit is given that |a-b|=k\n\t\t\tso a=k+b i.e secondNum=k+key\n\t\t\tSince object stores key as string so we have to typecast it to integer\n\t\t\tand +k is shorthand of parseInt(k)\n\t\t\tso we can write  parseInt(k)+parseInt(key) as\n\t\t\t+k + +key (make sure there is space between +/add operator)\n\t\t\t*/\n\t\t\tlet secondNum = +k + +key;\n\t\t\tif (map[secondNum] !== undefined) {\n\t\t\t\tcount++;\n\t\t\t}\n\t\t} else {\n\t\t\t/*So when k is 0 we check is num count is greater or equal\n\t\t\t to 2 because then only we will get difference of these two as 0\n\t\t\t */\n\t\t\tif (map[key] >= 2) count++;\n\t\t}\n\t});\n\treturn count;\n};\n```
['JavaScript']