Golang program to check a value exists in the hash collection or not


In golang we can check whether a given value exist in hash collection or not by simply using okidiom function or we can even create an if-else user-defined function to do the same. Hashmap is basically a collection of values paired with their keys in hash collection. In this article we are going to look over two different example to understand how we can use the above two techniques to check the given value in hash collection.

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 a hashmap using map literal where keys are of type string and values are of type int.

  • Assign the desired values to the keys.

  • In this step, use ok idiom to check if a particular mentioned key exists in the map, and if it exists its corresponding value can also be obtained.

  • Then, if the value exists in the map, ok will be set to true and the success statement is printed.

  • If value doesn’t exist, ok will be set to false and the failure statement will be printed.

  • The print statement is executed using Println() function from fmt package where ln means new line.

Example 1

In this example, we will create a hashmap with the help of map literal. Then, with the help of ok idiom we will see if the key exists and if the key exists, does the corresponding value to it exists? If the output is true and success statement is printed else failure statement is printed.

//Golang program to check if a value exists in the hash collection or not
package main

import "fmt"

//Main function to execute the program
func main() {
   
   // create a hashmap with key:value pairs
   hashmap := map[string]int{
      "pencil": 10,
      "pen":    20,
      "scale":  15,
   }
   
   fmt.Println("Whether the value exists in the map?") //if the value exists print if statement else print else statement
   if _, ok := hashmap["pencil"]; ok {
      fmt.Println("The value exists in the hash collection.")
   } else {
      fmt.Println("The value does not exist in the hash collection.")
   }
}

Output

Whether the value exists in the map?
The value exists in the hash collection.

Example 2

In this example, we will create a hashmap and iterate it to check if the value exists in it by using conditional if-else statement, if the condition is satisfied the success statement is printed and returned otherwise failure statement is printed.

//Golang program to check a value exists in the hash collection or not
package main

import (
   "fmt"
)
   
//Main function to execute the program
func main() {
   
   // create a hashmap with key:value pairs
   hashmap := map[string]string{
      "item1": "value1",
      "item2": "value2",
      "item3": "value3",
   }
   
   // check if a value exists in the hash collection
   fmt.Println("Whether the value exists in the map?")
   value := "value2"
   for _, v := range hashmap {
      if v == value {
         fmt.Println("Value exists in the hash collection")
         return
      }
   }
   
   fmt.Println("Value does not exist in the hash collection")
}

Output

Whether the value exists in the map?
Value exists in the hash collection

Conclusion

We executed this program of checking if the value exists int eh hash collection or not using two examples. In the first example we used ok idiom which further directed to true or false indicating the value exists or not whereas in the second example we used conditional statement to execute the program.

Updated on: 27-Mar-2023

455 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements