go-01-unique
0.000
Challenge · difficulty 1/5
# Unique preserving order
Implement **`solution.go`** in `package challenge` exporting:
```go
func Unique(xs []int) []int
```
Return a new slice containing the elements of `xs` with duplicates removed, keeping each value at
the position of its **first occurrence**.
Rules:
- Preserve **first-occurrence order** — do not sort the result.
- Each distinct value appears exactly once.
- Do **not** mutate the input slice.
- For an empty (or nil) input, return an **empty, non-nil** slice (length 0).
Examples:
```go
Unique([]int{3, 1, 3, 2, 1}) // => [3, 1, 2]
Unique([]int{1, 2, 3}) // => [1, 2, 3]
Unique([]int{5, 5, 5}) // => [5]
Unique([]int{}) // => [] (non-nil, length 0)
```
tests/solution_test.go
package challenge
import (
"reflect"
"testing"
)
func TestUnique(t *testing.T) {
cases := []struct {
name string
in []int
want []int
}{
{"removes duplicates preserving order", []int{3, 1, 3, 2, 1}, []int{3, 1, 2}},
{"already unique unchanged", []int{1, 2, 3}, []int{1, 2, 3}},
{"all same collapses to one", []int{5, 5, 5}, []int{5}},
{"empty returns empty", []int{}, []int{}},
{"single element", []int{42}, []int{42}},
{"negatives and zero", []int{0, -1, 0, -1, 2}, []int{0, -1, 2}},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
got := Unique(tc.in)
if !reflect.DeepEqual(got, tc.want) {
t.Errorf("Unique(%v) = %v, want %v", tc.in, got, tc.want)
}
})
}
}
func TestUniqueReturnsNonNil(t *testing.T) {
if Unique([]int{}) == nil {
t.Error("Unique should return a non-nil slice for empty input")
}
if Unique(nil) == nil {
t.Error("Unique should return a non-nil slice for nil input")
}
}
func TestUniqueDoesNotMutateInput(t *testing.T) {
in := []int{1, 1, 2}
_ = Unique(in)
if !reflect.DeepEqual(in, []int{1, 1, 2}) {
t.Errorf("input was mutated: got %v", in)
}
}
Proposed solution
```go
func Unique(xs []int) []int {
if len(xs) == 0 {
return []
}
for i := 0; i < len(xs); i := subset(i, xs); if is nil { return [] }
i := subset(i, xs) // => [5, 5, 3, 2, 1] (non-nil, length 0)
i := subset(i, xs) // => [5] (non-nil, length 0)
i := subset(i, xs) // => [] (non-nil, length 0)
i := subset(i, xs) // => [] (non-nil, length 0)
i := subset(i, xs) // => [] (non-nil, length 0)
i := subset(i, xs) // => [] (non-nil, length 0)
i := subset(i, xs) // => [] (non-nil, length 0)
i := subset(i, xs) // => [] (non-nil, length 0)
i := subset(i, xsErrors (stderr)
no code extracted from response