Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (50.2k points)

Here is my code:

H = hash()

H[["numbers"]] = c(1,2,3)

H[["alpha"]] = c("x","y","z")

H[["animals"]] = c("cat","dog")

Now from the above I want to get "animals" using "cat". Basically I want to get the key of the hash, based on the value.

1 Answer

0 votes
by (107k points)

For achieving your goal, you can use hash::invert in R programming, refer to the following code:

invert(H)[["cat"]]

[1] "animals"

And the above code will also works for multiple hashes with the same value.

H[["mammals"]] = c("cat","dog","human")

invert(H)[["cat"]]

[1] "animals" "mammals"

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...