亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Golang 從切片追加函數“已評估但未使用”中刪除重復整數

Golang 從切片追加函數“已評估但未使用”中刪除重復整數

Go
慕哥6287543 2023-04-24 16:01:58
我無法讓這個 Go lang 測試程序運行。編譯器在下面的 append() 函數調用中不斷給出錯誤,并顯示“已評估但未使用”錯誤。我不知道為什么。package mainimport (    "fmt")func removeDuplicates(testArr *[]int) int {    prevValue := (*testArr)[0]    for curIndex := 1; curIndex < len((*testArr)); curIndex++ {        curValue := (*testArr)[curIndex]        if curValue == prevValue {            append((*testArr)[:curIndex], (*testArr)[curIndex+1:]...)        }        prevValue = curValue    }    return len(*testArr)}func main() {    testArr := []int{0, 0, 1, 1, 1, 2, 2, 3, 3, 4}    nonDupSize := removeDuplicates(&testArr)    fmt.Printf("nonDupSize = %d", nonDupSize)}
查看完整描述

2 回答

?
qq_遁去的一_1

TA貢獻1725條經驗 獲得超8個贊

“已評估但未使用”錯誤。

下面的代碼是我的想法。我認為你的代碼不是很清楚。

package main


import (

? ? "fmt"

)


func removeDuplicates(testArr *[]int) int {

? ? m := make(map[int]bool)

? ? arr := make([]int, 0)


? ? for curIndex := 0; curIndex < len((*testArr)); curIndex++ {

? ? ? ? curValue := (*testArr)[curIndex]

? ? ? ? if has :=m[curValue]; !has {

? ? ? ? ? ? m[curValue] = true

? ? ? ? ? ? arr = append(arr, curValue)

? ? ? ? }

? ? }

? ? *testArr = arr

? ? return len(*testArr)

}


func main() {

? ? testArr := []int{0, 0, 1, 1, 1, 2, 2, 3, 3, 4}


? ? nonDupSize := removeDuplicates(&testArr)


? ? fmt.Printf("nonDupSize = %d", nonDupSize)

}


查看完整回答
反對 回復 2023-04-24
?
一只斗牛犬

TA貢獻1784條經驗 獲得超2個贊

編譯錯誤是由于沒有從 append() 獲取返回值



查看完整回答
反對 回復 2023-04-24
  • 2 回答
  • 0 關注
  • 138 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號