Go語言程式:從陣列中移除重複元素
本文將編寫一個 Go 語言程式,用於從陣列中移除重複元素。我們將使用兩種方法來實現這一點。第一種方法將使用字串陣列,第二種方法將使用整數陣列。
方法 1:使用 Make() 函式從字串陣列中移除重複元素
在本例中,我們將編寫一個 Go 語言程式,使用使用者定義函式從字串陣列中移除重複元素。該函式將接受字串陣列作為引數,並在移除重複值後返回最終陣列。
語法
func append(slice, element_1, element_2…, element_N) []T
append 函式用於向陣列切片新增值。它接受多個引數。第一個引數是要新增值的陣列,後面跟著要新增的值。然後,該函式返回包含所有值的最終陣列切片。
func make ([] type, size, capacity)
Go 語言中的 **make** 函式用於建立陣列/對映,它接受要建立的變數型別、其大小和容量作為引數
演算法
**步驟 1** - 首先,我們需要匯入 fmt 包。
**步驟 2** - 現在,建立一個名為 removeDuplicate() 的函式。此函式接受陣列作為引數,並返回包含唯一值集的結果。
**步驟 3** - 此函式使用 for 迴圈遍歷陣列。
**步驟 4** - 在這裡,我們建立了一個對映,其鍵為字串,值為布林值,預設情況下,對映儲存的值為假。
**步驟 5** - 在陣列的每次迭代中,我們檢查對映的值,如果它是假,則我們需要獲取該值並將其追加到上面建立的新陣列中,然後翻轉布林值。
**步驟 6** - 現在,我們需要啟動 main() 函式。
**步驟 7** - 初始化一個整數陣列 arr,向其中儲存值,並在螢幕上列印該陣列。
**步驟 8** - 現在,透過將上面建立的陣列作為引數傳遞給它,來呼叫 removeDuplicate 函式。
**步驟 9** - 將獲得的結果儲存在一個名為 result 的陣列中,並在螢幕上列印該陣列。
示例
使用外部函式從字串陣列中移除重複元素的 Go 語言程式
package main import "fmt" // function to remove duplicate values func removeDuplicates(s []string) []string { bucket := make(map[string]bool) var result []string for _, str := range s { if _, ok := bucket[str]; !ok { bucket[str] = true result = append(result, str) } } return result } func main() { // creating an array of strings array := []string{"abc", "cde", "efg", "efg", "abc", "cde"} fmt.Println("The given array of string is:", array) fmt.Println() // calling the function result := removeDuplicates(array) fmt.Println("The array obtained after removing the duplicate entries is:", result) }
輸出
The given array of string is: [abc cde efg efg abc cde] The array obtained after removing the duplicate entries is: [abc cde efg]
方法:使用 Append 函式從整數陣列中移除重複元素
以下程式碼說明了如何使用使用者定義函式從整數陣列中移除重複值
語法
func append(slice, element_1, element_2…, element_N) []T
append 函式用於向陣列切片新增值。它接受多個引數。第一個引數是要新增值的陣列,後面跟著要新增的值。然後,該函式返回包含所有值的最終陣列切片。
演算法
**步驟 1** - 首先,我們需要匯入 fmt 包。
**步驟 2** - 現在,建立一個名為 removeDuplicate() 的函式,該函式接受陣列作為引數,並在移除所有重複項後返回陣列。
**步驟 3** - 此函式使用 for 迴圈遍歷陣列。
**步驟 4** - 在這裡,我們建立了一個對映,其鍵為整數,值為布林值,預設情況下,對映 map_var 儲存的值為假。
**步驟 5** - 在陣列的每次迭代中,我們檢查 map_var 的值,如果它是假,則我們需要獲取該值並將其追加到上面建立的新陣列中
**步驟 6** - 重複此過程,直到檢查完所有陣列值,然後返回這樣形成的新陣列。
**步驟 7** - 現在,我們需要啟動 main 函式。
**步驟 8** - 初始化一個整數陣列 arr,向其中儲存值,並在螢幕上列印該陣列。
**步驟 9** - 現在,透過將上面建立的陣列作為引數傳遞給它,來呼叫 removeDuplicate 函式。
**步驟 10** - 將獲得的結果儲存在一個名為 result 的陣列中,並在螢幕上列印該陣列。
示例
使用 append 函式從整數陣列中移除重複元素的 Go 語言程式。
package main import "fmt" // making a function named removeDuplicate() func removeDuplicate(arr [8]int) []int { map_var := map[int]bool{} result := []int{} for e := range arr { if map_var[arr[e]] != true { map_var[arr[e]] = true result = append(result, arr[e]) } } return result } func main() { arr := [8]int{1, 2, 2, 4, 4, 5, 7, 5} fmt.Println("The unsorted array entered is:", arr) result := removeDuplicate(arr) fmt.Println("The array obtained after removing the duplicate values is:", result) }
輸出
The unsorted array entered is: [1 2 2 4 4 5 7 5] The array obtained after removing the duplicate values is: [1 2 4 5 7]
結論
我們已成功編譯並執行了一個 Go 語言程式,用於從陣列中移除重複值。我們在這裡使用了兩個程式,第一個程式從字串陣列中移除值,第二個程式從整數陣列中移除值。這兩個示例都使用使用者定義函式來實現結果。