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)。