remove hash from /arrays folder

This commit is contained in:
Vitor Oliveira 2021-05-14 09:44:08 -07:00 committed by GitHub
parent b4c2e2e6b0
commit 0a83001a2a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -40,37 +40,3 @@ puts(num_identical_pairs(nums))
nums = [1, 2, 3]
puts(num_identical_pairs(nums))
# Output: 0
#
# Approach 2: Hash
#
# Time Complexity: O(n)
def num_identical_pairs(nums)
hash = Hash.new(0)
nums.each do |num|
hash[num] = hash[num] + 1
end
counter = 0
# Count how many times each number appears.
# If a number appears n times, then n * (n 1) / 2 good pairs
# can be made with this number.
hash.values.each do |val|
counter += (val * (val - 1) / 2)
end
counter
end
nums = [1, 2, 3, 1, 1, 3]
puts(num_identical_pairs(nums))
# Output: 4
nums = [1, 1, 1, 1]
puts(num_identical_pairs(nums))
# Output: 6
nums = [1, 2, 3]
puts(num_identical_pairs(nums))
# Output: 0