how to delete Duplicate elements between slices on golang
up vote
-6
down vote
favorite
As a title,
how to delete duplicate elements between slices on golang
ex.
a_array := {"1","2","3","4,"}
b_array := {"3","4"}
hope of result
"1","2"
With the assumption, a_array elements definitely has b_array elements.
Please tell me how to do it.
Thank you!
go
add a comment |
up vote
-6
down vote
favorite
As a title,
how to delete duplicate elements between slices on golang
ex.
a_array := {"1","2","3","4,"}
b_array := {"3","4"}
hope of result
"1","2"
With the assumption, a_array elements definitely has b_array elements.
Please tell me how to do it.
Thank you!
go
Use afor
loop. tour.golang.org/moretypes/16
– Adrian
Nov 7 at 16:51
See this answer stackoverflow.com/a/15323988/1153938 for how to find a value in an array. Then loop it
– Vorsprung
Nov 7 at 17:14
Ask question more specifically. Do you want to delete the elements (those are in b_array) from a_array Or want to output like set operation A-B?
– Shudipta Sharma
Nov 7 at 17:28
add a comment |
up vote
-6
down vote
favorite
up vote
-6
down vote
favorite
As a title,
how to delete duplicate elements between slices on golang
ex.
a_array := {"1","2","3","4,"}
b_array := {"3","4"}
hope of result
"1","2"
With the assumption, a_array elements definitely has b_array elements.
Please tell me how to do it.
Thank you!
go
As a title,
how to delete duplicate elements between slices on golang
ex.
a_array := {"1","2","3","4,"}
b_array := {"3","4"}
hope of result
"1","2"
With the assumption, a_array elements definitely has b_array elements.
Please tell me how to do it.
Thank you!
go
go
asked Nov 7 at 16:44
marumo
1
1
Use afor
loop. tour.golang.org/moretypes/16
– Adrian
Nov 7 at 16:51
See this answer stackoverflow.com/a/15323988/1153938 for how to find a value in an array. Then loop it
– Vorsprung
Nov 7 at 17:14
Ask question more specifically. Do you want to delete the elements (those are in b_array) from a_array Or want to output like set operation A-B?
– Shudipta Sharma
Nov 7 at 17:28
add a comment |
Use afor
loop. tour.golang.org/moretypes/16
– Adrian
Nov 7 at 16:51
See this answer stackoverflow.com/a/15323988/1153938 for how to find a value in an array. Then loop it
– Vorsprung
Nov 7 at 17:14
Ask question more specifically. Do you want to delete the elements (those are in b_array) from a_array Or want to output like set operation A-B?
– Shudipta Sharma
Nov 7 at 17:28
Use a
for
loop. tour.golang.org/moretypes/16– Adrian
Nov 7 at 16:51
Use a
for
loop. tour.golang.org/moretypes/16– Adrian
Nov 7 at 16:51
See this answer stackoverflow.com/a/15323988/1153938 for how to find a value in an array. Then loop it
– Vorsprung
Nov 7 at 17:14
See this answer stackoverflow.com/a/15323988/1153938 for how to find a value in an array. Then loop it
– Vorsprung
Nov 7 at 17:14
Ask question more specifically. Do you want to delete the elements (those are in b_array) from a_array Or want to output like set operation A-B?
– Shudipta Sharma
Nov 7 at 17:28
Ask question more specifically. Do you want to delete the elements (those are in b_array) from a_array Or want to output like set operation A-B?
– Shudipta Sharma
Nov 7 at 17:28
add a comment |
1 Answer
1
active
oldest
votes
up vote
-1
down vote
If you need to strictly compare one slice against the other you may do something along the lines of
func diff(a string, b string) string {
// Turn b into a map
var m map[string]bool
m = make(map[string]bool, len(b))
for _, s := range b {
m[s] = false
}
// Append values from the longest slice that don't exist in the map
var diff string
for _, s := range a {
if _, ok := m[s]; !ok {
diff = append(diff, s)
continue
}
m[s] = true
}
// Sort the resulting slice
sort.Strings(diff)
return diff
}
Go Playground
Alternatively if you want to get all values from both slices that are not present in both of them you can do
func diff(a string, b string) string {
var shortest, longest *string
if len(a) < len(b) {
shortest = &a
longest = &b
} else {
shortest = &b
longest = &a
}
// Turn the shortest slice into a map
var m map[string]bool
m = make(map[string]bool, len(*shortest))
for _, s := range *shortest {
m[s] = false
}
// Append values from the longest slice that don't exist in the map
var diff string
for _, s := range *longest {
if _, ok := m[s]; !ok {
diff = append(diff, s)
continue
}
m[s] = true
}
// Append values from map that were not in the longest slice
for s, ok := range m {
if ok {
continue
}
diff = append(diff, s)
}
// Sort the resulting slice
sort.Strings(diff)
return diff
}
Then
fmt.Println(diff(a_array, b_array))
will give you
[1 2]
Go playground
Dif can't be done based longest or smallest sized set. That means, lets sayA = {1, 2}
andB = {1, 2, 3, 4}
and if he wantsA dif B
(AB
or ` A - B` in terms of set), then your program just give output{3, 4}
where the correct answer is{}
. Hope make sense.
– Shudipta Sharma
Nov 8 at 13:08
First of all the suggested implementation is not based on shortest or smallest, it's just an implementation detail for a slightly different problem - get values from both slices that are not present in both of them or in other words eliminate all that present in both and give me the rest unique values, again, from both slices. From OP's question it's not clear what exactly is required - the result of subtracting two slices pretending they are sets or my interpretation. But I'll add another version that strictly comparesa
againstb
.
– peterm
Nov 8 at 19:11
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
-1
down vote
If you need to strictly compare one slice against the other you may do something along the lines of
func diff(a string, b string) string {
// Turn b into a map
var m map[string]bool
m = make(map[string]bool, len(b))
for _, s := range b {
m[s] = false
}
// Append values from the longest slice that don't exist in the map
var diff string
for _, s := range a {
if _, ok := m[s]; !ok {
diff = append(diff, s)
continue
}
m[s] = true
}
// Sort the resulting slice
sort.Strings(diff)
return diff
}
Go Playground
Alternatively if you want to get all values from both slices that are not present in both of them you can do
func diff(a string, b string) string {
var shortest, longest *string
if len(a) < len(b) {
shortest = &a
longest = &b
} else {
shortest = &b
longest = &a
}
// Turn the shortest slice into a map
var m map[string]bool
m = make(map[string]bool, len(*shortest))
for _, s := range *shortest {
m[s] = false
}
// Append values from the longest slice that don't exist in the map
var diff string
for _, s := range *longest {
if _, ok := m[s]; !ok {
diff = append(diff, s)
continue
}
m[s] = true
}
// Append values from map that were not in the longest slice
for s, ok := range m {
if ok {
continue
}
diff = append(diff, s)
}
// Sort the resulting slice
sort.Strings(diff)
return diff
}
Then
fmt.Println(diff(a_array, b_array))
will give you
[1 2]
Go playground
Dif can't be done based longest or smallest sized set. That means, lets sayA = {1, 2}
andB = {1, 2, 3, 4}
and if he wantsA dif B
(AB
or ` A - B` in terms of set), then your program just give output{3, 4}
where the correct answer is{}
. Hope make sense.
– Shudipta Sharma
Nov 8 at 13:08
First of all the suggested implementation is not based on shortest or smallest, it's just an implementation detail for a slightly different problem - get values from both slices that are not present in both of them or in other words eliminate all that present in both and give me the rest unique values, again, from both slices. From OP's question it's not clear what exactly is required - the result of subtracting two slices pretending they are sets or my interpretation. But I'll add another version that strictly comparesa
againstb
.
– peterm
Nov 8 at 19:11
add a comment |
up vote
-1
down vote
If you need to strictly compare one slice against the other you may do something along the lines of
func diff(a string, b string) string {
// Turn b into a map
var m map[string]bool
m = make(map[string]bool, len(b))
for _, s := range b {
m[s] = false
}
// Append values from the longest slice that don't exist in the map
var diff string
for _, s := range a {
if _, ok := m[s]; !ok {
diff = append(diff, s)
continue
}
m[s] = true
}
// Sort the resulting slice
sort.Strings(diff)
return diff
}
Go Playground
Alternatively if you want to get all values from both slices that are not present in both of them you can do
func diff(a string, b string) string {
var shortest, longest *string
if len(a) < len(b) {
shortest = &a
longest = &b
} else {
shortest = &b
longest = &a
}
// Turn the shortest slice into a map
var m map[string]bool
m = make(map[string]bool, len(*shortest))
for _, s := range *shortest {
m[s] = false
}
// Append values from the longest slice that don't exist in the map
var diff string
for _, s := range *longest {
if _, ok := m[s]; !ok {
diff = append(diff, s)
continue
}
m[s] = true
}
// Append values from map that were not in the longest slice
for s, ok := range m {
if ok {
continue
}
diff = append(diff, s)
}
// Sort the resulting slice
sort.Strings(diff)
return diff
}
Then
fmt.Println(diff(a_array, b_array))
will give you
[1 2]
Go playground
Dif can't be done based longest or smallest sized set. That means, lets sayA = {1, 2}
andB = {1, 2, 3, 4}
and if he wantsA dif B
(AB
or ` A - B` in terms of set), then your program just give output{3, 4}
where the correct answer is{}
. Hope make sense.
– Shudipta Sharma
Nov 8 at 13:08
First of all the suggested implementation is not based on shortest or smallest, it's just an implementation detail for a slightly different problem - get values from both slices that are not present in both of them or in other words eliminate all that present in both and give me the rest unique values, again, from both slices. From OP's question it's not clear what exactly is required - the result of subtracting two slices pretending they are sets or my interpretation. But I'll add another version that strictly comparesa
againstb
.
– peterm
Nov 8 at 19:11
add a comment |
up vote
-1
down vote
up vote
-1
down vote
If you need to strictly compare one slice against the other you may do something along the lines of
func diff(a string, b string) string {
// Turn b into a map
var m map[string]bool
m = make(map[string]bool, len(b))
for _, s := range b {
m[s] = false
}
// Append values from the longest slice that don't exist in the map
var diff string
for _, s := range a {
if _, ok := m[s]; !ok {
diff = append(diff, s)
continue
}
m[s] = true
}
// Sort the resulting slice
sort.Strings(diff)
return diff
}
Go Playground
Alternatively if you want to get all values from both slices that are not present in both of them you can do
func diff(a string, b string) string {
var shortest, longest *string
if len(a) < len(b) {
shortest = &a
longest = &b
} else {
shortest = &b
longest = &a
}
// Turn the shortest slice into a map
var m map[string]bool
m = make(map[string]bool, len(*shortest))
for _, s := range *shortest {
m[s] = false
}
// Append values from the longest slice that don't exist in the map
var diff string
for _, s := range *longest {
if _, ok := m[s]; !ok {
diff = append(diff, s)
continue
}
m[s] = true
}
// Append values from map that were not in the longest slice
for s, ok := range m {
if ok {
continue
}
diff = append(diff, s)
}
// Sort the resulting slice
sort.Strings(diff)
return diff
}
Then
fmt.Println(diff(a_array, b_array))
will give you
[1 2]
Go playground
If you need to strictly compare one slice against the other you may do something along the lines of
func diff(a string, b string) string {
// Turn b into a map
var m map[string]bool
m = make(map[string]bool, len(b))
for _, s := range b {
m[s] = false
}
// Append values from the longest slice that don't exist in the map
var diff string
for _, s := range a {
if _, ok := m[s]; !ok {
diff = append(diff, s)
continue
}
m[s] = true
}
// Sort the resulting slice
sort.Strings(diff)
return diff
}
Go Playground
Alternatively if you want to get all values from both slices that are not present in both of them you can do
func diff(a string, b string) string {
var shortest, longest *string
if len(a) < len(b) {
shortest = &a
longest = &b
} else {
shortest = &b
longest = &a
}
// Turn the shortest slice into a map
var m map[string]bool
m = make(map[string]bool, len(*shortest))
for _, s := range *shortest {
m[s] = false
}
// Append values from the longest slice that don't exist in the map
var diff string
for _, s := range *longest {
if _, ok := m[s]; !ok {
diff = append(diff, s)
continue
}
m[s] = true
}
// Append values from map that were not in the longest slice
for s, ok := range m {
if ok {
continue
}
diff = append(diff, s)
}
// Sort the resulting slice
sort.Strings(diff)
return diff
}
Then
fmt.Println(diff(a_array, b_array))
will give you
[1 2]
Go playground
edited Nov 8 at 20:26
answered Nov 7 at 19:27
peterm
72.6k12100113
72.6k12100113
Dif can't be done based longest or smallest sized set. That means, lets sayA = {1, 2}
andB = {1, 2, 3, 4}
and if he wantsA dif B
(AB
or ` A - B` in terms of set), then your program just give output{3, 4}
where the correct answer is{}
. Hope make sense.
– Shudipta Sharma
Nov 8 at 13:08
First of all the suggested implementation is not based on shortest or smallest, it's just an implementation detail for a slightly different problem - get values from both slices that are not present in both of them or in other words eliminate all that present in both and give me the rest unique values, again, from both slices. From OP's question it's not clear what exactly is required - the result of subtracting two slices pretending they are sets or my interpretation. But I'll add another version that strictly comparesa
againstb
.
– peterm
Nov 8 at 19:11
add a comment |
Dif can't be done based longest or smallest sized set. That means, lets sayA = {1, 2}
andB = {1, 2, 3, 4}
and if he wantsA dif B
(AB
or ` A - B` in terms of set), then your program just give output{3, 4}
where the correct answer is{}
. Hope make sense.
– Shudipta Sharma
Nov 8 at 13:08
First of all the suggested implementation is not based on shortest or smallest, it's just an implementation detail for a slightly different problem - get values from both slices that are not present in both of them or in other words eliminate all that present in both and give me the rest unique values, again, from both slices. From OP's question it's not clear what exactly is required - the result of subtracting two slices pretending they are sets or my interpretation. But I'll add another version that strictly comparesa
againstb
.
– peterm
Nov 8 at 19:11
Dif can't be done based longest or smallest sized set. That means, lets say
A = {1, 2}
and B = {1, 2, 3, 4}
and if he wants A dif B
( AB
or ` A - B` in terms of set), then your program just give output {3, 4}
where the correct answer is {}
. Hope make sense.– Shudipta Sharma
Nov 8 at 13:08
Dif can't be done based longest or smallest sized set. That means, lets say
A = {1, 2}
and B = {1, 2, 3, 4}
and if he wants A dif B
( AB
or ` A - B` in terms of set), then your program just give output {3, 4}
where the correct answer is {}
. Hope make sense.– Shudipta Sharma
Nov 8 at 13:08
First of all the suggested implementation is not based on shortest or smallest, it's just an implementation detail for a slightly different problem - get values from both slices that are not present in both of them or in other words eliminate all that present in both and give me the rest unique values, again, from both slices. From OP's question it's not clear what exactly is required - the result of subtracting two slices pretending they are sets or my interpretation. But I'll add another version that strictly compares
a
against b
.– peterm
Nov 8 at 19:11
First of all the suggested implementation is not based on shortest or smallest, it's just an implementation detail for a slightly different problem - get values from both slices that are not present in both of them or in other words eliminate all that present in both and give me the rest unique values, again, from both slices. From OP's question it's not clear what exactly is required - the result of subtracting two slices pretending they are sets or my interpretation. But I'll add another version that strictly compares
a
against b
.– peterm
Nov 8 at 19:11
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53194031%2fhow-to-delete-duplicate-elements-between-slices-on-golang%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Use a
for
loop. tour.golang.org/moretypes/16– Adrian
Nov 7 at 16:51
See this answer stackoverflow.com/a/15323988/1153938 for how to find a value in an array. Then loop it
– Vorsprung
Nov 7 at 17:14
Ask question more specifically. Do you want to delete the elements (those are in b_array) from a_array Or want to output like set operation A-B?
– Shudipta Sharma
Nov 7 at 17:28