```
int findPairs(vector<int>& nums, int k) {
int count=0;
sort(nums.begin(),nums.end());
for(int i=0;i<nums.size();i++){
if(find(nums.begin()+i+1,nums.end(),nums[i]+k)!=nums.end()) count++;
while(nums[i+1]==nums[i]) i++;
}
return count;
}
```