Thursday, November 29, 2018

Learn Golang Tutorias Set Data structure with examples

This post covers Implementation of Set Datastrucutre in go language with examples

Golang Set Introduction

Set is a general data strcuture contains collection of elements, does not allow duplicate elements. some programming languages like java has inbuilt Set support
In Programming language, Set is a computer data structure implementation of mathematical finiteset concepts.Generally Set is a Unordered list and no duplicates. In Golang, We can use Map type to implement Set type.
You can implement set using Map types.
 Golang has no built in support for set because of no generics avialable. We can write a custom code to write a Map Implementation 
Syntax 
var myset map[type]struct{} or
var myset map[type]bool
we can go with map[type]struct{} than a map[type]bool, the reason is empty struct takes 0 bytes, bool takes 1 byte of size in memory.
Key is type of the data you want to create
Struct{} is empty struct takes 0 bytes in size
Like other datastructure, set also operations - Create a set, iterate over those elements, add elements,delete elements, clear the set,size of set, check element exists in a set.
We wills see basic functions of a set
  • Create and Declaration of a set
 For example Let us create Empty Set with accept string values
s1 := make(map[string]struct{})
This is empty set with string key and empty struct - struct{}
  • Add the elements of Set 
 You can add the elements to set using add element to map syntax map declare a empty struct, use this as a value in a set map and key as string
	var empty = struct{}{}
	s1["one"] = empty
	s1["two"] = empty
	s1["three"] = empty
	fmt.Println(s1)      // map[three:{} one:{} two:{}]
	fmt.Println(len(s1)) // 3
	
The abolve code add three elements to set Once data is added to set, We will see iteration
  • Iteration of elements of a Set 
You can iterate elements of a set using for loop with range form
for v := range s1 {
		fmt.Println(v)
	}
	
When you run the above code multiple times, Order of the elements are not guarenteed, Output is as follows
one
two
three
  • Check if elements exists in a set 
We can check elements in a set using two values expression using getting items from map
_, ok := s1["one"];
This returns two values first value is empty struct, not required, so blank identifier(_) is used in place second parameters is a boolen value - if exists, ok=true is retured, ok=false is returned, if not exists

if _, ok := s1["one"]; ok {
		fmt.Println("exists in a set ") //  Element exists
	}
	if _, ok := s1["ten"]; ok {
		fmt.Println("Not exists in a set.") //  Element not exists
	}
And the output is
exists in a set 
  • Delete a elment of a set
You can use delete() function of map to remove elements from a set
delete(s1, "one")
	fmt.Println(len(s1)) // 2

Set Example  - Basic Operations

Here is a complete Set Example with all basic operations
package main

import (
	"fmt"
)

func main() {
	s1 := make(map[string]struct{})
	fmt.Println(s1) // map[]
	//Adding elements to Set
	var empty = struct{}{}
	s1["one"] = empty
	s1["two"] = empty
	s1["three"] = empty
	fmt.Println(s1)      // map[three:{} one:{} two:{}]
	fmt.Println(len(s1)) // 3

	for v := range s1 {
		fmt.Println(v)
	}

	if _, ok := s1["one"]; ok {
		fmt.Println("exists in a set ") //  Element exists
	}
	if _, ok := s1["ten"]; ok {
		fmt.Println("Not exists in a set.") //  Element not exists
	}
	delete(s1, "one")
	fmt.Println(len(s1)) // 2

}
Output is
map[]
map[two:{} three:{} one:{}]
3
one
two
three
exists in a set
2

Related article


EmoticonEmoticon