- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Golang Program To Convert Array To Set
In this tutorial, we will learn how to convert an array to a set using the Golang programming language.
A Set is a collection of items. You can iterate on these items / add new / remove items and clear the set and get the size and check if the set contains any value. An object in the set might only be stored once and cannot duplicate.
Syntax
var myset map[type]struct{}
Key is a type of data you want to create. Struct{} is an empty struct that takes 0 bytes in size.
Example 1: Go Code to Convert an Array to a Set Using For Loop
Algorithm
STEP 1 − Import the package fmt.
STEP 2 − Start function main ().
STEP 3 − Create a set ‘a1’ and declare it.
STEP 4 − Add elements of the array to the set a1.
STEP 5 − Using for loop with range fore to iterate the elements.
STEP 6 − Check if elements exist in the set.
STEP 7 − Use of delete () function to remove elements.
STEP 8 − Print the final results on the screen using fmt.Println ().
Example
package main // fmt package allows us to print anything on the screen import "fmt" // start the function main() // this function is the entry point of the executable program func main() { // Create and Declaration of a set // a1 acts as a set of strings a1 := make(map[string]struct{}) fmt.Println(a1) // map[] //Adding elements to Set // by setting the value of each key to an // empty struct var empty = struct{}{} a1["five"] = empty a1["four"] = empty a1["six"] = empty fmt.Println(a1) // map[five:{} four:{} six:{}] fmt.Println(len(a1)) // 3 //Iteration of elements of a Set using for loop with a range form for v := range a1 { fmt.Println(v) } // Check if elements exist in a set if _, ok := a1["five"]; ok { fmt.Println("exists in a set ") // Element exists } if _, ok := a1["one"]; ok { fmt.Println("Not exists in a set.") // Element not exists } // use the delete() function of the map to remove elements from a set delete(a1, "five") fmt.Println(len(a1)) // 2 // print the final results }
Output
map[] map[five:{} four:{} six:{}] 3 five four six exists in a set 2
Description
In the above program, we first declare the package main. The main package is used to tell the Go language compiler that the package must be compiled and produced the executable file.
We imported the fmt package that includes the files of the package fmt then we can use a function related to the fmt package
Now start the function main () and this function is the entry point of the executable program. It does not take any argument nor return anything.
Now we create an Empty a set ‘a1’ which accepts string values and declares it.Here s1:= make(map[string]struct{}) is an empty set with a string key and empty struct - struct{}
Next, we add the elements to the set using add an element to map syntax map to declare an empty struct. In lines 23, 24 and 25 of the code, the code adds three elements to the set Once data is added to the set
Next, we iterate elements of a set using for loop with a range form.
Next, we will check elements in the set using two values expressions using getting items from the map. In line 35 of the code: if _, ok:= a1["five"]; ok { :: Here it returns two values first value is an empty struct, not required, so blank identifier(_) is used in place the second parameter is a boolean value - if exists, ok=true is returned, ok=false is returned, if not exists.
Next, we use the delete() function of the map to remove elements from a set. We can remove an item from a map using Go's built-in delete () function.
Finally we print the results using fmt.Println () function
We were able to execute all operations that are characteristic of a set, with the same time complexity of O(1) for adding and removing members from a set.
Example 2: a Simpler go Code to Convert an Array to a Set
Algorithm
STEP 1 − Import the package fmt.
STEP 2 − Start function main ().
STEP 3 − Create a set ‘fruit’ and declare it.
STEP 4 − Add elements of the array to the set fruit.
STEP 5 − Print the final results on the screen using fmt.Println ().
Example
// GOLANG PROGRAM TO CONVERT ARRAY TO SET // We can implement set using Map types. // Declare the package main package main // fmt package allows us to print anything on the screen import "fmt" // start the function main () // this function is the entry point of the executable program func main() { // Create and Declaration of a set // fruit acts as a set of strings fruit := map[string]struct{}{} fmt.Println("fruit") // We can add members to the set // by setting the value of each key to an // empty struct fruit["apple"] = struct{}{} fruit["banana"] = struct{}{} fruit["Grapes"] = struct{}{} fruit["Orange"] = struct{}{} // Adding a new member fruit["Kiwi"] = struct{}{} // Adding an existing to the set fruit["Grapes"] = struct{}{} // Removing a member delete(fruit, "banana") fmt.Println(fruit) }
Output
fruit map[Grapes:{} Kiwi:{} Orange:{} apple:{}]
Description
In the above code, we have created a set using array elements and shown a simpler code without use of any conditional statements. And we were able to execute all operations that are characteristic of a set, with the same time complexity of O (1) for adding and removing members from a set.