How to Reimplement the Unix Uniq Command in Golang

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"]
Code language: PHP (php)

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 }
Code language: Go (go)

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 }
Code language: Go (go)

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] }
Code language: Go (go)

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"})) }) })
Code language: Go (go)
Tags:
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments