Interesting Java Solution/ HashSet Only
Put all numbers n in Hashset S1.\nPut all numbers n+k in HashSet S2.\nThe number of pairs are the intersection of the two Hashsets. Different conditions apply t
Put all numbers n in Hashset S1.\nPut all numbers n+k in HashSet S2.\nThe number of pairs are the intersection of the two Hashsets. Different conditions apply to k=0 or k<0.\n\n```\npublic class Solution {\n    public int findPairs(int[] nums, int k) {\n        \n        int ans = 0;\n        \n        if(k<0) return ans;\n        \n        Set<Integer> set1 = new HashSet<Integer> ();\n        Set<Integer> set2 = new HashSet<Integer> ();\n        \n        if(k==0){\n                for(int n:nums){\n                    if(!set1.contains(n))\n                        {set1.add(n);}\n                    else{\n                        set1.remove(n);\n                        if(!set2.contains(n)) ans++;\n                        set2.add(n);\n                        }\n                }\n        }\n        else{\n            for(int n:nums){\n                set1.add(n);\n                set2.add(n+k);\n            }\n            set1.retainAll(set2);\n            ans = set1.size();\n        }\n        \n        return ans;\n    }\n}\n```
[]