In Go, I am using int
s to represent elements of a set, []int
to represent a subset, and [][]int
to represent a set of subsets. A two identical []int
slices (subsets) should not be allowed. Thus given some var setOfSubets [][]int
, I would like to detect duplicate []int
slices in setOfSubets
. What is an efficient idomatic way to detect these duplicates?
For my program, I will return an error when the first duplicate is detected.
As an example, a user could create setOfSubsets
setOfSubsets := make([][]int, 0)
subSetA := []int{0, 1} // 0 and 1 are elements
subSetB := []int{0, 2} // 0 and 2 are elements
subSetADuplicate := []int{0, 1} // 0 and 1 are elements
setOfSubsets = append(setOfSubsets, subSetA)
setOfSubsets = append(setOfSubsets, subSetB)
setOfSubsets = append(setOfSubsets, subSetADuplicate)
then my program should detect that {0, 1}
is duplicated. In the real program, there could be thousands of elements and perhaps millions of subsets.
I thought of using a map with []int
as the key but I realized that is not allowed in Go. I also considered sorting the set of subsets and then checking adjacent subsets []int
slices but I am not sure that is idiomatic or efficient.