
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Calculate Symmetric Difference Between Two Slices in Golang
Slice is similar to an array, the only difference is that an array is a fixed sequence of elements whereas slice the array elements are dynamic. This makes the slice more efficient and faster in various applications. In the slice, the elements are passed by reference instead of values. In this article, we are going to learn about different techniques to calculate the symmetric difference between two slice using Golang programming.
Algorithm
Step 1 ? Create a package main and declare fmt(format package) package in the program where main produces executable codes and fmt helps in formatting input and output.
Step 2 ? Create a main function and in that function create two slices slice1 and slice2 and print those slices using print statement in Golang.
Step 3 ? Call the function symmetric difference from the main function, the entire program will be executed inside the helper function.
Step 4 ? In the function symmetric difference create a diff slice in which the elements will be appended.
Step 5 ? Iterate the loop till the range of slice1 and set the initial value of found as false.
Step 6 ? Iterate the second slice till the range of slice2 and check if the elements of slice1 are equal to elements of slice2, set the found equals to true and break the loop.
Step 7 ? If the element is not found append the element of slice1 in the diff slice.
Step 8 ? Iterate the loop till the range of slice2 and set the initial value of found as false.
Step 9 ? Iterate the first slice till the range of slice1 and check if the elements of slice2 are equal to elements of slice1, set the found equals to true and break the loop.
Step 10 ? If the element is not found append the element of slice2 in the diff slice.
Step 11 ? Return the diff slice to the function and print it on the console using fmt.Println() function where ln means new line.
Example 1
In this example we will learn how to calculate the symmetric difference between two slices using for loops twice on the slice1 and slice2 and then append those elements which are not common in both slices.
package main import "fmt" func main() { // create slice1 myslice1 := []int{10, 20, 30, 40, 50} fmt.Println("The elements of slice1 are:", myslice1) // create slice2 myslice2 := []int{30, 40, 50, 60, 70} fmt.Println("The elements of slice2 are:", myslice2) // to find symmetric differance diff := symmetricdifference(myslice1, myslice2) fmt.Println("The symmetric difference of two slices is:") fmt.Println(diff) } func symmetricdifference(myslice1, myslice2 []int) []int { var diff []int // to iterate on the element of first number slice for _, val1 := range myslice1 { found := false // Iterate over elements in second slice for _, elem2 := range myslice2 { if val1 == elem2 { found = true break } } if !found { diff = append(diff, val1) } } // to iterate on the elements present in slice number 2 for _, elem2 := range myslice2 { found := false for _, elem1 := range myslice1 { if elem2 == elem1 { found = true break } } if !found { diff = append(diff, elem2) } } // to return the symmetric difference return diff }
Output
The elements of slice1 are: [10 20 30 40 50] The elements of slice2 are: [30 40 50 60 70] The symmetric difference of two slices is: [10 20 60 70]
Example 2
In this example, we will see how to calculate the symmetric difference between two slices using map which is constructed using make function in Golang which is a built-in function.
package main import ( "fmt" ) func main() { myslice1 := []int{10, 20, 30, 40, 50} //create slice1 fmt.Println("The elements of slice1 are:", myslice1) myslice2 := []int{30, 40, 50, 60, 70} //create slice2 fmt.Println("The elements of slice2 are:", myslice2) diff := symmetricdifference(myslice1, myslice2) fmt.Println("The symmetric difference between two slices are:") fmt.Println(diff) } func symmetricdifference(myslice1, myslice2 []int) []int { // Create a map to store the elements of the first slice map_store := make(map[int]bool) for _, val := range myslice1 { map_store[val] = true } var diff []int // Iterate over elements in second slice for _, elem := range myslice2 { if _, ok := map_store[elem]; !ok { diff = append(diff, elem) } else { delete(map_store, elem) } } // Add remaining keys in the map for key := range map_store { diff = append(diff, key) } return diff //return symmetric difference }
Output
The elements of slice1 are: [10 20 30 40 50] The elements of slice2 are: [30 40 50 60 70] The symmetric difference between two slices are: [60 70 10 20]
Conclusion
We executed the program of calculating the symmetric difference between two slices using two examples. In the first example we used two times for loop on slices and in the second example we used map to store values and find symmetric difference.