A
A
Adatan2019-12-13 17:50:53
go
Adatan, 2019-12-13 17:50:53

How to find the difference of two slices in Golang?

Suppose I have two slices:
slice1 := []string{"123", "12", "13"}
slice2 := []string{"123, "12, "13", "23}
I need to compare get them and the extra value that is not in the other one (the values ​​inside can be in any order).
And I will always know exactly which of the slices will be larger.
In python, I would just make two sets and subtract the other from one)
But here, my powers cannot come up with a normal algorithm (and I tried a lot)....

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman Kitaev, 2019-12-13
@deliro

If you still need to find the difference of the sets (that is, without binding the elements to their positions), then you need to do something like this https://play.golang.org/p/EWsmVji_3aA
If you do as Pardon Me suggests! Where Do I Find 4giveness? , then your result will "crawl" after the first "extra" like this: https://play.golang.org/p/pNVboCOaU__Q

L
Leonid Nikolaev, 2019-12-13
@nikonor

If the arrays are not large, then make a map and count the occurrences.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question