Java O(n) solution with comments
class Solution {\n    public int findPairs(int[] nums, int k) {\n        \n       int output=0;\n       HashMap  hm = new HashMap<>(); \n        \n       //Stor
class Solution {\n    public int findPairs(int[] nums, int k) {\n        \n       int output=0;\n       HashMap <Integer,Integer> hm = new HashMap<>(); \n        \n       //Storing the frequencies in the hashmpap\n        for(int num: nums)\n        {\n            hm.put(num, hm.getOrDefault(num,0)+1);\n        }\n        \n        \n        //Traversing the hashmap\n        for (Map.Entry <Integer, Integer> entry: hm.entrySet()) \n        {\n            int elem = entry.getKey();\n            int value = entry.getValue();\n            \n            \n            // finding some other element (elem+ other element=k)\n            if(k>0 && hm.containsKey(elem+k))\n                output++;\n            \n            // The number can be paired with itself. Hence, the value should be >1. We don\'t care what value as long as the frequency is greater than one as the question says "k-diff pair".\n            else if(k==0 && value >1)\n                output++;\n            \n        }\n        return output;\n    }\n}
[]