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!










share|improve this question






















  • 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

















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!










share|improve this question






















  • 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















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!










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 7 at 16:44









marumo

1




1












  • 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




















  • 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


















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














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






share|improve this answer























  • 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













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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

























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






share|improve this answer























  • 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

















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






share|improve this answer























  • 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















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






share|improve this answer














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







share|improve this answer














share|improve this answer



share|improve this answer








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 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




















  • 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


















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




















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini