Go 语言程序:将数组转换为集合
在本教程中,我们将学习如何使用 Go 编程语言将数组转换为集合。
集合是一组项目的集合。您可以迭代这些项目/添加新项目/删除项目和清除集合,并获取大小以及检查集合是否包含任何值。集合中的对象可能只能存储一次,并且不能重复。
语法
var myset map[type]struct{}
Key 是您要创建的数据类型。Struct{} 是一个空结构体,占用 0 字节大小。
示例 1:使用 For 循环将数组转换为集合的 Go 代码
算法
步骤 1 - 导入 fmt 包。
步骤 2 - 开始函数 main()。
步骤 3 - 创建一个集合 'a1' 并声明它。
步骤 4 - 将数组的元素添加到集合 a1 中。
步骤 5 - 使用带有范围 for 的 for 循环迭代元素。
步骤 6 - 检查元素是否存在于集合中。
步骤 7 - 使用 delete() 函数删除元素。
步骤 8 - 使用 fmt.Println() 在屏幕上打印最终结果。
示例
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
}
输出
map[]
map[five:{} four:{} six:{}]
3
five
four
six
exists in a set
2
描述
在上面的程序中,我们首先声明了 main 包。main 包用于告诉 Go 语言编译器必须编译该包并生成可执行文件。
我们导入了 fmt 包,其中包含 fmt 包的文件,然后我们可以使用与 fmt 包相关的函数
现在开始函数 main(),此函数是可执行程序的入口点。它不接受任何参数也不返回值。
现在我们创建一个空的集合 'a1',它接受字符串值并声明它。这里 s1:= make(map[string]struct{}) 是一个带有字符串键和空结构体 - struct{} 的空集合
接下来,我们使用添加元素到 map 语法 map 来添加元素到集合中以声明一个空结构体。在代码的第 23、24 和 25 行,代码将三个元素添加到集合中,一旦数据添加到集合中
接下来,我们使用带有范围形式的 for 循环迭代集合的元素。
接下来,我们将使用两个值表达式从 map 中获取项目来检查集合中的元素。在代码的第 35 行:if _, ok:= a1["five"]; ok { :: 它返回两个值,第一个值是一个空结构体,不需要,因此在第二个参数中使用空白标识符(_),第二个参数是一个布尔值 - 如果存在,则返回 ok=true,如果不存在,则返回 ok=false。
接下来,我们使用 map 的 delete() 函数从集合中删除元素。我们可以使用 Go 的内置 delete() 函数从 map 中删除项目。
最后,我们使用 fmt.Println() 函数打印结果
我们能够执行所有集合特有的操作,并且添加和删除集合成员的时间复杂度相同,为 O(1)。
示例 2:一个更简单的 Go 代码将数组转换为集合
算法
步骤 1 - 导入 fmt 包。
步骤 2 - 开始函数 main()。
步骤 3 - 创建一个集合 'fruit' 并声明它。
步骤 4 - 将数组的元素添加到集合 fruit 中。
步骤 5 - 使用 fmt.Println() 在屏幕上打印最终结果。
示例
// 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)
}
输出
fruit
map[Grapes:{} Kiwi:{} Orange:{} apple:{}]
描述
在上面的代码中,我们使用数组元素创建了一个集合,并显示了一个更简单的代码,无需使用任何条件语句。并且我们能够执行所有集合特有的操作,并且添加和删除集合成员的时间复杂度相同,为 O(1)。
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP