Golang program to get the hash elements as a sorted array from the hash collection


In golang we can obtain the hash elements as a sorted array from the hash collection using iteration, sort package as well as slice function.

A hash collection contains a hashmap which is used to store items in the form of key value pairs. A hashmap is implemented using a hash function.

Syntax

func make ([] type, size, capacity)

The make function in go language is used to create an array/map it accepts the type of variable to be created, its size and capacity as arguments

func append(slice, element_1, element_2…, element_N) []T

The append function is used to add values to an array slice. It takes number of arguments. The first argument is the array to which we wish to add the values followed by the values to add. The function then returns the final slice of array containing all the values.

funcStrings(src []string) []string

The strings function is defined in sort package. This function takes the array in string format that we wish to sort and returns the result by sorting that array.

sort.Slice()

This function is the part of the sort package. It is used to sort the slice of integers. It takes two parameters the slice which is to be sorted and one function which returns a Boolean value.

func range(variable)

The range function is used to iterate over any data type. To use this, we first have to write the range keyword followed by the data type to which we want to iterate and as a result the loop will iterate till the last element of the variable.

Algorithm

  • Step 1 − This program imports two packages fmt, main and sort package where fmt helps in the formatting of input and output and main helps in generating executable codes

  • Step 2 − Create a main function

  • Step 3 − In the main, create a hashmap using map literal with keys of type string and values of type int

  • Step 4 − Then, create a keys slice using make as built-in function similar to the length of hashmap

  • Step 5 − Iterate the hashmap and append its keys in the slice

  • Step 6 − Then, use Strings function from the sort package to sort the keys slice

  • Step 7 − Print the sorted slice on the console

  • Step 8 − The print statement is executed using Println function from the fmt package where ln means new line

Example 1

In this Example we will create a hashmap with keys of type string and values of type int. Then, keys slice will be created similar to the length of hashmap. Iterate the hashmap and add the keys of hashmap in the keys slice.

package main

import (
   "fmt"
   "sort"
)

func main() {
   hashmap := map[string]int{
      "noodles": 110,
      "momos":   80,
      "pizza":   190,
      "burger":  50,
   }

   keys := make([]string, 0, len(hashmap))
   for k := range hashmap {
      keys = append(keys, k)
   }
   sort.Strings(keys)
   fmt.Println("The sorted array is given as:")
   fmt.Println(keys)
}

Output

The sorted array is given as:
[burger momos noodles pizza]

Example 2

In this Example, a hashmap will be created using map literal. Use Strings function from sort package to sort the keys in the slice. Finally, print the sorted array on the terminal.

package main

import (
   "fmt"
   "sort"
)

func main() {
   hashmap := map[string]int{
      "noodles": 110,
      "momos":   60,
      "pizza":   250,
      "pasta":   80,
   }

   keys := make([]string, len(hashmap))
   i := 0
   for k := range hashmap {
      keys[i] = k
      i++
   }

   sort.Strings(keys)  //sort the array

   fmt.Println("The sorted array is given as:")
   fmt.Println(keys)
}

Output

The sorted array is given as:
[momos noodles pasta pizza]

Example 3

In this illustration, we will create a hashmapand we will use Slice function from the sort package to sort the keys in the new slice. The sorted array will be printed on the console.

package main

import (
   "fmt"
   "sort"
)

func main() {
   hashmap := map[string]int{
      "noodles": 110,
      "pasta":   60,
      "pizza":   290,
      "momos":   40,
   }

   keys := make([]string, 0, len(hashmap))
   for k := range hashmap {
      keys = append(keys, k)
   }

   sort.Slice(keys, func(i, j int) bool {
      return keys[i] < keys[j]
   })

   fmt.Println("The sorted array is given as:")
   fmt.Println(keys)
}

Output

The sorted array is given as:
[momos noodles pasta pizza]

Conclusion

We compiled and executed the program of getting the hash elements as sorted array. In the first Example we will use Strings function from the sort package, in the second Example we will use an index variable in the loop with the similar Strings function and in the third Example we will use Slice function from the sort package. All the Examples returned sorted array.

Updated on: 03-May-2023

170 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements