Golang program to get key based on value from the hash collection


In golang, we can use hashmap to perform various operations like deleting, updating, and storing the values in the hash collection. In this article, we are going to understand get key based on a value from the hash collection using two different examples. In the first example, we will use an external function and iterate the map to get the key from the value, in the second example we will use a reverse map to get the key from the desired value.

Algorithm

  • Create a package main and declare fmt(format package) in the program where main produces executable codes and fmt helps in formatting input and output.

  • Create an external function named get_value_from_key with two inputs the hashmap and the value whose key is to be obtained.

  • Iterate the hashmap and on every iteration check whether the value is equal to the val of the hashmap.

  • If its true return the key whose value is checked but if the value is not present in the hashmap return empty string.

  • In the main function, a hashmap is created using map literal where the keys are of type string and values are of type int.

  • Call the external function from the main with two arguments and obtain the returned value in key.

  • Call the function again with a value that is not present in the hashmap and an empty string will be returned in this case.

  • Print the key on the console using fmt.Println() function where ln means new line.

Example 1

In this example, we will create an external function with two parameters namely the hashmap and the value whose key is to be printed.

package main

import "fmt"

func get_value_from_key(hash map[string]int, value int) string {
   for key, val := range hash {
      if val == value {   //check if the value is equal to the val from hashmap return the key
         return key
      }
   }
   return ""
}
   
func main() {
   
   // Example hash collection
   hashmap := map[string]int{
      "one":   10,
      "two":   20,
      "three": 30,
   }
   
   // obtain key for value 2
   key := get_value_from_key(hashmap, 20)
   fmt.Println("The key to corresponding value is:")
   fmt.Println(key)  
   
   // obtain key for value 4 (not found)
   key = get_value_from_key(hashmap, 40)
   fmt.Println(key)
}

Output

The key to corresponding value is:
two

Example 2

In this illustration, we will create a map in which we will assign the keys to the values in reverse manner then we will return the respective key to the value given as input. The output will be the key to the respective value. Let’s explore it through the code and the algorithm.

package main
   
import "fmt"
   
func get_key_from_value(hash map[string]int, value int) string {
   reverseHash := make(map[int]string)
   for key, val := range hash {
      reverseHash[val] = key  //use map reversal to add the leys to the following values
   }
   return reverseHash[value]  //return the key following to this value
}

func main() {

   // Example hash collection
   hashmap := map[string]int{  
      "one":   10,
      "two":   20,
      "three": 30,
   }
   
   // obtain key for value 2
   key := get_key_from_value(hashmap, 20)
   fmt.Println("The key of the above value is: ")
   fmt.Println(key) // Output: two
   
   // obtain key for value 4 (not found)
   key = get_key_from_value(hashmap, 40)
   fmt.Println(key) // Output:
}

Output

The key of the above value is: 
two

Conclusion

We executed the program of getting a key based on a value from hash collection using two methods. In the first example we used an external function and iteration to get the required key and in the second example we used a reverse map technique to get the desired output.

Updated on: 27-Mar-2023

287 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements