Skip to content

Instantly share code, notes, and snippets.

@kernelshreyak
Created June 6, 2021 08:51
Show Gist options
  • Save kernelshreyak/0f2ba4bb0244181848feae3bdeb9414f to your computer and use it in GitHub Desktop.
Save kernelshreyak/0f2ba4bb0244181848feae3bdeb9414f to your computer and use it in GitHub Desktop.
Array Diff in Golang
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);
}
@kernelshreyak
Copy link
Author

Input:
{1,2,3,5} and {1,3,2,5,6,7} :
Output:
Array diff: [7 6]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment