Skip to content

Instantly share code, notes, and snippets.

@tquach
Last active August 29, 2015 14:07
Show Gist options
  • Select an option

  • Save tquach/89821d35dbc71dcc3e7b to your computer and use it in GitHub Desktop.

Select an option

Save tquach/89821d35dbc71dcc3e7b to your computer and use it in GitHub Desktop.
Compare two slices of strings for equality in O(n) time.
package slices
// Equal compares two slices of strings for equality in less than polynomial time worst case.
func Equal(s1, s2 []string) bool {
if len(s1) != len(s2) {
return false
}
o := make(map[string]int)
for _, x := range s1 {
o[x]++
}
for _, y := range s2 {
o[y]--
if o[y] == 0 {
delete(o, y)
}
}
return len(o) == 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment