The challenge

Implement a function which behaves like the uniq command in UNIX.

It takes as input a sequence and returns a sequence in which all duplicate elements following each other have been reduced to one instance.

Example:

["a", "a", "b", "b", "c", "a", "b", "c"]  =>  ["a", "b", "c", "a", "b", "c"]

The solution in Golang

Option 1:

package uniq
func Uniq(a []string) []string {
 array := []string{}
  for i, value := range a {
    if i == 0 {
      array = append(array, value)
    } else if a[i-1] != value {
      array = append(array, value)
    }
  }
  return array
}

Option 2:

package uniq
func Uniq(a []string) []string {
  answer := make([]string, 0, len(a))
  for i, str := range a {
    if i == 0 || str != a[i-1] {
      answer = append(answer, str)
    }
  }
  return answer
}

Option 3:

package uniq
import "fmt"
func Uniq(a []string) []string {
  n := len(a)
  if (n == 0) {
    return []string{}
  }
  i := 1
  for j := 1; j < n; j++ {
    if (a[i] == a[j] || a[i - 1] == a[j]) {
      continue;
    } else {
      a[i] = a[j]
      i++
    }
  }
  fmt.Println(i)
  return a[0:i]
}

Test cases to validate our solution

package uniq_test
import (
  . "github.com/onsi/ginkgo"
  . "github.com/onsi/gomega"
)
var _ = Describe("Test Suite", func() {
  It("Sample Tests", func() {
    Expect(Uniq([]string{"a", "a", "b", "b", "c", "a", "b", "c", "c"})).To(Equal([]string{"a", "b", "c", "a", "b", "c"}))
    Expect(Uniq([]string{"a", "a", "a", "b", "b", "b", "c", "c", "c"})).To(Equal([]string{"a", "b", "c"}))
    Expect(Uniq([]string{})).To(Equal([]string{}))
    Expect(Uniq([]string{"foo"})).To(Equal([]string{"foo"}))
    Expect(Uniq([]string{"bar"})).To(Equal([]string{"bar"}))
    Expect(Uniq([]string{""})).To(Equal([]string{""}))
    Expect(Uniq([]string{"a", "a"})).To(Equal([]string{"a"}))
  })
})