0

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)
}
gtalton007
  • 21
  • 3

2 Answers2

0

"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)
}
GerryLon
  • 64
  • 4
0

Peter's answer nailed it, the compile error was due to not grabbing the return value from append()

gtalton007
  • 21
  • 3