How to find a hash key containing a matching value?
Last Updated :
10 Apr, 2024
In this article, we will discuss how to find a hash key containing a matching value in ruby. We can find a hash key containing a matching value through different methods ranging from using Hash#key
method and Hash#select
method to any? method
Finding a hash key containing a matching value using the Hash#key
The Hash#key method returns the first key in the hash that has the matching value
Syntax:
hash.key(value)
Example: In this example, we use Hash#key
to search for the key containing the value 2 in the example_hash where it returns the first key found, which is :b.
Ruby
# Define a method to find a hash key containing a matching value using Hash#key.
def find_key_with_value(hash, value)
# Use the key method to find the key containing the specified value.
hash.key(value)
end
# Example hash
example_hash = { a: 1, b: 2, c: 3, d: 1 }
# Find the key containing the value 2 in the example hash
result = find_key_with_value(example_hash, 2)
# Output the result
puts "Key containing value 2 is #{result}"
OutputKey containing value 2 is b
Find a hash key containing a matching value using Hash#select
The Hash#select
method returns a new hash consisting of key-value pairs for which the block returns true.
Syntax:
hash.select { |key, value| value == desired_value }.keys
Example: In this example,we use Hash#select
method to searches for keys containing the value 1 in the example_hash and thus returning array of keys that match the condition, which are [:a, :d].
Ruby
# Define a method to find a hash key containing a matching value using Hash#select.
def find_key_with_value(hash, value)
# Use select to filter key-value pairs where the value matches the desired value, then extract the keys.
hash.select { |key, val| val == value }.keys
end
# Example hash
example_hash = { a: 1, b: 2, c: 3, d: 1 }
# Find the key(s) containing the value 1 in the example hash
result = find_key_with_value(example_hash, 1)
# Output the result
puts "Key(s) containing value 1 are #{result}"
OutputKey(s) containing value 1 are [:a, :d]
Find a hash key containing a matching value using Enumerable#find
The Enumerable#find method returns the first key-value pair for which the block returns true
Syntax:
hash.find { |key, value| value == desired_value }&.first
Example: In this example we use the Enumerable#find
method is used to search for the first key containing the value 3 in the example_hash. It returns the first key found, which is :c.
Ruby
# Define a method to find a hash key containing a matching value using Enumerable#find.
def find_key_with_value(hash, value)
# Use find to search for the first key-value pair where the value matches the desired value, then extract the key.
hash.find { |key, val| val == value }&.first
end
# Example hash
example_hash = { a: 1, b: 2, c: 3, d: 1 }
# Find the first key containing the value 3 in the example hash
result = find_key_with_value(example_hash, 3)
# Output the result
puts "First key containing value 3 is #{result}"
OutputMethod 3: First key containing value 3 is c
Share your thoughts in the comments
Please Login to comment...