3 java solutions: 1.HashMap without sort; 2.HashMap + sort; 3. Sort + two points
HashMap without sort.\nWe define a hashMap. The key element denotes the members of the array. The value has 3 types:\nvalue 1: the key appears once in the array
1. HashMap without sort.\nWe define a hashMap. The key element denotes the members of the array. The value has 3 types:\nvalue 1: the key appears once in the array\nvalue 2: the key appears more than once in the array\nvalue 0: the key has been used, this value is used to avoid duplicated pairs.\nWe use a for-loop to initial the hashMap. \nIf k == 0, we just calculate the number which appears more than once in array.\nif k != 0,  each number adds k and subtracts k, then using the result to match the map. \n```\nclass Solution {\n    public int findPairs(int[] nums, int k) {\n        if(k<0)return 0;\n        Map<Integer,Integer> map = new HashMap<>();\n        for(int i = 0; i<nums.length; i++){\n            if(map.containsKey(nums[i])){\n                map.put(nums[i],2);\n            }else{\n                map.put(nums[i],1);\n            }\n        }\n        int count = 0;\n        if(k==0){\n            for(int i = 0; i<nums.length; i++){\n                if(map.containsKey(nums[i]) && map.get(nums[i]) > 1){\n                    count++;\n                    map.replace(nums[i],0);\n                }\n            }\n            return count;\n        }\n        for(int i = 0; i<nums.length; i++){\n            if(map.containsKey(nums[i] + k) && map.get(nums[i] + k) > 0 && map.get(nums[i]) > 0){\n                count++;\n            }\n            if(map.containsKey(nums[i] - k) && map.get(nums[i] - k) > 0 && map.get(nums[i]) > 0){\n                count++;\n            }\n            map.replace(nums[i],0);\n        }\n        return count;\n    }\n}\n```\n\n2. HashMap + Sort\nThis solution is similar to the first solution. \nWe firstly sort the array, so we don\'t need to consider multiple situations(k==0 or k != 0 ; add or subtract). The sort reduces the complexity of this problem.\n```\nclass Solution {\n    public int findPairs(int[] nums, int k) {\n        if(k < 0)return 0;\n        Map<Integer, Boolean> map = new HashMap<>();\n        int count = 0;\n        Arrays.sort(nums);\n        for(int i = 0; i < nums.length; i++){\n            if(map.containsKey(nums[i]) && map.get(nums[i])){\n                count++;\n                map.replace(nums[i],false);\n            }\n            if(!map.containsKey(nums[i] + k)){\n                map.put(nums[i] + k, true);\n            }\n        }\n        return count;\n    }\n}\n```\n\n3.Sort + Two Points:\nThis solution is based on the ordering of the array.\n```\nclass Solution {\n    public int findPairs(int[] nums, int k) {\n        if(k < 0) return 0;\n        Arrays.sort(nums);\n        int left = 0, right = 1, count = 0;\n        while(right < nums.length){\n            if(nums[right] - nums[left] > k){\n                left++;\n            }else if(nums[right] - nums[left] < k || right == left){\n                right++;\n            }else{\n                count++;\n                left++;\n                right++;\n                while(right < nums.length && nums[right] == nums[right - 1]) right++;\n            }\n        }\n        return count;\n    }\n}\n```
['Java']