Last active
October 3, 2020 17:12
-
-
Save mashiro/f19c9053c0a17055275380fa9c656a8e to your computer and use it in GitHub Desktop.
K-diff Pairs in an Array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# @param {Integer[]} nums | |
# @param {Integer} k | |
# @return {Integer} | |
def find_pairs(nums, k) | |
if k == 0 | |
return nums.group_by { |n| n } | |
.map { |k, v| [k, v.size] } | |
.filter { |(n, c)| c >= 2 } | |
.size | |
end | |
a = Set.new nums | |
a.inject(0) { |m, n| a.include?(n + k) ? m + 1 : m } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment