Created
August 10, 2017 09:09
-
-
Save luojiyin1987/67d0d01d312c0ad903d69c6af389ff8f to your computer and use it in GitHub Desktop.
Intersection of Two Arrays
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
| func intersection(nums1 []int, nums2 []int) []int { | |
| var temp []int | |
| if len(nums1) == 0 || len(nums2)==0 { | |
| return temp | |
| } | |
| temp1 := make(map[int]int) | |
| temp2 := make(map[int]int) | |
| for _, v := range nums1 { | |
| temp1[v] = v | |
| } | |
| for _,v := range nums2 { | |
| temp2[v] =1 | |
| } | |
| fmt.Println(temp1) | |
| fmt.Println(temp2) | |
| for k ,v := range temp1 { | |
| //_ , ok1 := temp1[k] | |
| _ , ok2 := temp2[k] | |
| if ok2 { | |
| temp = append(temp, v) | |
| } | |
| } | |
| return temp | |
| } | |
| func intersection(nums1 []int, nums2 []int) []int { | |
| m := make(map[int]bool) | |
| for _,num := range nums1 { | |
| m[num] = true | |
| } | |
| var v []int | |
| for _,num := range nums2 { | |
| if m[num] { | |
| v = append(v, num) | |
| delete(m,num) | |
| } | |
| } | |
| return v | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment