douya2433 2018-10-13 20:27
浏览 60
已采纳

Golang从“附加但未使用”的切片附加函数中删除dup int

I can't get this Go lang test program to run. The compiler keeps giving an error on the append() function call below with an "evaluated but not used" error. I can't figure out why.

package main

import (
    "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条回答 默认 最新

  • douxun2023 2018-10-17 04:15
    关注

    "evaluated but not used" error.

    Code below is my idea. I think your code is not very clear.

    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)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用