-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[xslices] added
Unique
function (#3)
- Loading branch information
1 parent
386c5cf
commit b79199f
Showing
5 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ use ( | |
./xmaps | ||
./xmath | ||
./xnet/xhttp | ||
./xslices | ||
./xslog | ||
./xstrconv | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# xslices | ||
Extensions to the standard Go library's `slices` package. | ||
|
||
### Install | ||
``` | ||
go get github.com/kucherenkovova/gopypaste/[email protected] | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module github.com/kucherenkovova/gopypaste/xslices | ||
|
||
go 1.22 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package xslices | ||
|
||
// Unique returns a new slice containing only the unique elements of the input slice. | ||
// Only first occurrence of each element is preserved. | ||
func Unique[T comparable](list []T) []T { | ||
if list == nil { | ||
return nil | ||
} | ||
|
||
seen := make(map[T]struct{}, len(list)) | ||
results := make([]T, 0, len(list)) | ||
|
||
for _, element := range list { | ||
if _, ok := seen[element]; ok { | ||
continue | ||
} | ||
|
||
seen[element] = struct{}{} | ||
results = append(results, element) | ||
} | ||
|
||
return results | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package xslices_test | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/kucherenkovova/gopypaste/xslices" | ||
) | ||
|
||
func TestUnique(t *testing.T) { | ||
type testCase struct { | ||
name string | ||
in []string | ||
want []string | ||
} | ||
|
||
tests := []testCase{ | ||
{ | ||
name: "empty list", | ||
in: []string{}, | ||
want: []string{}, | ||
}, | ||
{ | ||
name: "nil", | ||
in: nil, | ||
want: nil, | ||
}, | ||
{ | ||
name: "no duplicates", | ||
in: []string{"a", "b", "c"}, | ||
want: []string{"a", "b", "c"}, | ||
}, | ||
{ | ||
name: "consequent duplicates", | ||
in: []string{"a", "a", "b", "b", "c", "c"}, | ||
want: []string{"a", "b", "c"}, | ||
}, | ||
{ | ||
name: "non-consequent duplicates", | ||
in: []string{"a", "b", "a", "c", "b", "c"}, | ||
want: []string{"a", "b", "c"}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := xslices.Unique(tt.in); !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("Unique() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |