Created
June 6, 2021 08:51
-
-
Save kernelshreyak/0f2ba4bb0244181848feae3bdeb9414f to your computer and use it in GitHub Desktop.
Array Diff in Golang
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 main | |
import( | |
"fmt" | |
) | |
func main(){ | |
arr1 := []int{1,2,3,5} | |
arr2 := []int{1,3,2,5,6,7} | |
found := make(map[int]bool) | |
var result []int | |
for i := 0; i < len(arr1); i++ { | |
found[arr1[i]] = true; | |
} | |
for i := 0; i < len(arr2); i++ { | |
if(!found[arr2[i]]){ | |
found[arr2[i]] = false; | |
}else{ | |
delete(found,arr2[i]) | |
} | |
} | |
for key,value := range found { | |
if(!value){ | |
result = append(result,key) | |
} | |
} | |
fmt.Println("Array diff: ",result); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Input:
{1,2,3,5} and {1,3,2,5,6,7} :
Output:
Array diff: [7 6]