Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in the array. Here a k-diff pair is defined as an integer pair (i, j), where i and j are both numbers in the array and their absolute difference is k.
classSolution { public: intfindPairs(vector<int>& nums, int k){ if(k < 0) return0; int res = 0, n = nums.size(); unordered_map<int, int> m; for (int num : nums) ++m[num]; if(k){ for (auto a : m) if (m.count(a.first + k)) ++res; } else { for (auto a : m) if (a.second > 1) ++res; } return res; } };