Return Unique items in a Go slice
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
2
down vote
favorite
I'm trying to return unique items in a Go slice. I've landed on the following, but it seems rather sub-optimal. I'm new to golang (first project in it) and wondering if there's a better way to approach this?
//
// Returns unique items in a slice
//
func Unique(strSlice string) string
keys := make(map[string]bool)
list := string
for _, entry := range strSlice
if _, value := keys[entry]; !value
keys[entry] = true
list = append(list, entry)
return list
performance go
add a comment |Â
up vote
2
down vote
favorite
I'm trying to return unique items in a Go slice. I've landed on the following, but it seems rather sub-optimal. I'm new to golang (first project in it) and wondering if there's a better way to approach this?
//
// Returns unique items in a slice
//
func Unique(strSlice string) string
keys := make(map[string]bool)
list := string
for _, entry := range strSlice
if _, value := keys[entry]; !value
keys[entry] = true
list = append(list, entry)
return list
performance go
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I'm trying to return unique items in a Go slice. I've landed on the following, but it seems rather sub-optimal. I'm new to golang (first project in it) and wondering if there's a better way to approach this?
//
// Returns unique items in a slice
//
func Unique(strSlice string) string
keys := make(map[string]bool)
list := string
for _, entry := range strSlice
if _, value := keys[entry]; !value
keys[entry] = true
list = append(list, entry)
return list
performance go
I'm trying to return unique items in a Go slice. I've landed on the following, but it seems rather sub-optimal. I'm new to golang (first project in it) and wondering if there's a better way to approach this?
//
// Returns unique items in a slice
//
func Unique(strSlice string) string
keys := make(map[string]bool)
list := string
for _, entry := range strSlice
if _, value := keys[entry]; !value
keys[entry] = true
list = append(list, entry)
return list
performance go
asked Apr 4 at 11:12
Codingo
8422826
8422826
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
3
down vote
accepted
This is not idiomatic writing style in Go, because the value
variable is misleading:
if _, value := keys[entry]; !value {
The first returned value is the value in the map, the second value indicates success or failure of the lookup.
So rename it to ok
or found
.
If the slice is very large,
then list = append(list, entry)
may lead to repeated allocations.
In that case, you can optimize by preallocating list
to the maximum possible capacity (len(strSlice)
), and assign elements by index rather than using append
.
But if you have no reason for this optimization then don't do it,
that would be premature optimization,
the current solution is fine.
add a comment |Â
up vote
0
down vote
Instead of creating you final slice during the loop, you could do two loops: one for creating the map
and another to turn it into a slice:
//
// Returns unique items in a slice
//
func Unique(slice string) string
// create a map with all the values as key
uniqMap := make(map[string]struct)
for _, v := range slice
uniqMap[v] = struct
// turn the map keys into a slice
uniqSlice := make(string, 0, len(uniqMap))
for v := range uniqMap
uniqSlice = append(uniqSlice, v)
return uniqSlice
If your are not interested into the value of the map, it is customary to store the empty struct
.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
This is not idiomatic writing style in Go, because the value
variable is misleading:
if _, value := keys[entry]; !value {
The first returned value is the value in the map, the second value indicates success or failure of the lookup.
So rename it to ok
or found
.
If the slice is very large,
then list = append(list, entry)
may lead to repeated allocations.
In that case, you can optimize by preallocating list
to the maximum possible capacity (len(strSlice)
), and assign elements by index rather than using append
.
But if you have no reason for this optimization then don't do it,
that would be premature optimization,
the current solution is fine.
add a comment |Â
up vote
3
down vote
accepted
This is not idiomatic writing style in Go, because the value
variable is misleading:
if _, value := keys[entry]; !value {
The first returned value is the value in the map, the second value indicates success or failure of the lookup.
So rename it to ok
or found
.
If the slice is very large,
then list = append(list, entry)
may lead to repeated allocations.
In that case, you can optimize by preallocating list
to the maximum possible capacity (len(strSlice)
), and assign elements by index rather than using append
.
But if you have no reason for this optimization then don't do it,
that would be premature optimization,
the current solution is fine.
add a comment |Â
up vote
3
down vote
accepted
up vote
3
down vote
accepted
This is not idiomatic writing style in Go, because the value
variable is misleading:
if _, value := keys[entry]; !value {
The first returned value is the value in the map, the second value indicates success or failure of the lookup.
So rename it to ok
or found
.
If the slice is very large,
then list = append(list, entry)
may lead to repeated allocations.
In that case, you can optimize by preallocating list
to the maximum possible capacity (len(strSlice)
), and assign elements by index rather than using append
.
But if you have no reason for this optimization then don't do it,
that would be premature optimization,
the current solution is fine.
This is not idiomatic writing style in Go, because the value
variable is misleading:
if _, value := keys[entry]; !value {
The first returned value is the value in the map, the second value indicates success or failure of the lookup.
So rename it to ok
or found
.
If the slice is very large,
then list = append(list, entry)
may lead to repeated allocations.
In that case, you can optimize by preallocating list
to the maximum possible capacity (len(strSlice)
), and assign elements by index rather than using append
.
But if you have no reason for this optimization then don't do it,
that would be premature optimization,
the current solution is fine.
answered Apr 6 at 22:03
janos
95.5k12120343
95.5k12120343
add a comment |Â
add a comment |Â
up vote
0
down vote
Instead of creating you final slice during the loop, you could do two loops: one for creating the map
and another to turn it into a slice:
//
// Returns unique items in a slice
//
func Unique(slice string) string
// create a map with all the values as key
uniqMap := make(map[string]struct)
for _, v := range slice
uniqMap[v] = struct
// turn the map keys into a slice
uniqSlice := make(string, 0, len(uniqMap))
for v := range uniqMap
uniqSlice = append(uniqSlice, v)
return uniqSlice
If your are not interested into the value of the map, it is customary to store the empty struct
.
add a comment |Â
up vote
0
down vote
Instead of creating you final slice during the loop, you could do two loops: one for creating the map
and another to turn it into a slice:
//
// Returns unique items in a slice
//
func Unique(slice string) string
// create a map with all the values as key
uniqMap := make(map[string]struct)
for _, v := range slice
uniqMap[v] = struct
// turn the map keys into a slice
uniqSlice := make(string, 0, len(uniqMap))
for v := range uniqMap
uniqSlice = append(uniqSlice, v)
return uniqSlice
If your are not interested into the value of the map, it is customary to store the empty struct
.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Instead of creating you final slice during the loop, you could do two loops: one for creating the map
and another to turn it into a slice:
//
// Returns unique items in a slice
//
func Unique(slice string) string
// create a map with all the values as key
uniqMap := make(map[string]struct)
for _, v := range slice
uniqMap[v] = struct
// turn the map keys into a slice
uniqSlice := make(string, 0, len(uniqMap))
for v := range uniqMap
uniqSlice = append(uniqSlice, v)
return uniqSlice
If your are not interested into the value of the map, it is customary to store the empty struct
.
Instead of creating you final slice during the loop, you could do two loops: one for creating the map
and another to turn it into a slice:
//
// Returns unique items in a slice
//
func Unique(slice string) string
// create a map with all the values as key
uniqMap := make(map[string]struct)
for _, v := range slice
uniqMap[v] = struct
// turn the map keys into a slice
uniqSlice := make(string, 0, len(uniqMap))
for v := range uniqMap
uniqSlice = append(uniqSlice, v)
return uniqSlice
If your are not interested into the value of the map, it is customary to store the empty struct
.
answered Apr 25 at 21:52
oliverpool
1,542425
1,542425
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f191238%2freturn-unique-items-in-a-go-slice%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password