为了账号安全,请及时绑定邮箱和手机立即绑定

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条经验 获得超7个赞

“已评估但未使用”错误。

下面的代码是我的想法。我认为你的代码不是很清楚。

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 关注
  • 83 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信