Simple java O(n) solution with explaination
\npublic int findPairs(int[] nums, int k) {\n        HashMap<Integer,Integer> map = new HashMap<>();\n        int res=0;\n        //storing count of all the ele
``` \npublic int findPairs(int[] nums, int k) {\n        HashMap<Integer,Integer> map = new HashMap<>();\n        int res=0;\n        //storing count of all the elements\n        for(int i:nums){\n            map.put(i,map.getOrDefault(i,0)+1);\n        }\n        for(int a:map.keySet()){\n            if(k!=0){\n                //we avoid duplicate by looking for the values (i,j) where i is smaller element eg k=4 (1,3) & (3,1) gives same result but when we are processing 1 we are lloking fr val greater than 1 to form the pair and likewise when we are at 3we are looking for val 3 and above to form the pair\n                int b=a+k;\n                if(map.containsKey(b)){\n                    res++;\n                }\n            }else{\n                //processing the key so processing unique values which is occuring twice  and diff of it gives 0\n                if(map.get(a)>=2)\n                    res++;\n            }\n        }\n        return res;\n    }\n\t```
['Java']