TheAlgorithms-Ruby/data_structures/hash_table/good_pairs.rb
Vitor Oliveira 8bb781f790 Minor fixes
2021-09-03 13:24:58 -07:00

43 lines
827 B
Ruby
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Challenge name: Number of good pairs
#
# Given an array of integers nums.
# A pair (i,j) is called good if nums[i] == nums[j] and i < j.
# Return the number of good pairs.
#
# @param {Integer[]} nums
# @return {Integer}
#
#
# Approach 1: 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