Last active
August 29, 2015 14:07
-
-
Save tquach/89821d35dbc71dcc3e7b to your computer and use it in GitHub Desktop.
Compare two slices of strings for equality in O(n) time.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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