[C++] [Java] Clean Code with Explanation [set] [map]
C++\n\nclass Solution {\npublic:\n    /**\n     * for every number in the array:\n     *  - if there was a number previously k-diff with it, save the smaller to
**C++**\n```\nclass Solution {\npublic:\n    /**\n     * for every number in the array:\n     *  - if there was a number previously k-diff with it, save the smaller to a set;\n     *  - and save the value-index to a map;\n     */\n    int findPairs(vector<int>& nums, int k) {\n        if (k < 0) {\n            return 0;\n        }\n        unordered_set<int> starters;\n        unordered_map<int, int> indices;\n        for (int i = 0; i < nums.size(); i++) {\n            if (indices.count(nums[i] - k)) {\n                starters.insert(nums[i] - k);\n            }\n            if (indices.count(nums[i] + k)) {\n                starters.insert(nums[i]);\n            }\n\n            indices[nums[i]] += 1;\n        }\n        \n        return starters.size();\n    }\n};\n```\n**Java**\n```\npublic class Solution {\n    public int findPairs(int[] nums, int k) {\n        if (k < 0) { return 0; }\n\n        Set<Integer> starters = new HashSet<Integer>();\n        Set<Integer> uniqs = new HashSet<Integer>();\n        for (int i = 0; i < nums.length; i++) {\n            if (uniqs.contains(nums[i] - k)) starters.add(nums[i] - k);\n            if (uniqs.contains(nums[i] + k)) starters.add(nums[i]);\n            uniqs.add(nums[i]);\n        }\n\n        return starters.size();\n    }\n}\n```
[]