- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Golang program to compare elements in two slices
In this tutorial, we will learn how to compare elements in two slices. In slices a simple equality comparison is not possible so the slices are compared with their lengths and the elements present in the loop. The output will be printed in the form of Boolean value on the console with the help of fmt.Println() function. Let’s see how to execute this with the help of an example.
Method 1: Using a user-defined function
In this method, we will compare elements in two slices using an external function and, in that function, we will set some conditions, if the slices satisfy those conditions, they will be considered equal else they won’t be considered equal. Let’s have a look to get a better understanding.
Syntax
func append(slice, element_1, element_2…, element_N) []T
The append function is used to add values to an array slice. It takes number of arguments. The first argument is the array to which we wish to add the values followed by the values to add. The function then returns the final slice of array containing all the values.
Algorithm
Step 1 − Create a package main and import fmt package in the program.
Step 2 − Create a main function, in it create two slices of type string and call a function named slice_equality with two slices as arguments.
Step 3 − Create a function slice_equality and in that function check if the length of the first slice is not equal to the second slice return false.
Step 4 − In the next case run a for loop till the range of str1 and check if the elements of str2 are equal to str1, if they are not equal return false.
Step 5 − After checking all the conditions set in the algorithm, if not even once false is returned, return true to the function.
Step 6 − Print the Boolean value using fmt.Println() function where ln refers to the next line here.
Example
Golang program to compare elements in two slices using custom function
package main import "fmt" func slice_equality(str1, str2 []string) bool { if len(str1) != len(str2) { return false } for i, str := range str1 { if str != str2[i] { return false } } return true // return true if the slices are equal } func main() { str1 := []string{"Goa", "Gujarat"} // create slice1 str2 := []string{"Goa", "Gujarat"} // create sice2 fmt.Println("The slices are equal or not before adding any element:") fmt.Println(slice_equality(str1, str2)) // printing slice equality before adding any element str2 = append(str2, "Mumbai") fmt.Println("The slices are equal or not after adding another element:") fmt.Println(slice_equality(str1, str2)) // printing slice equality after adding extra element }
Output
The slices are equal or not before adding any element: true The slices are equal or not after adding another element: false
Method 2: Using built-in function
In this method, we will use reflect.DeepEqual() function to compare two slices recursively. Built-in functions ease our work and shorten the code. The output here will be printed using fmt.Println() function. Let’s have a look and inculcate how to solve this problem.
Syntax
reflect.DeepEqual()
This function compares two values recursively. It traverses and check the equality of the corresponding data values at each level. However, the solution is less safe as compared to comparison in loops. Reflect should be used with care and should be used in those cases where it’s of utmost importance.
func append(slice, element_1, element_2…, element_N) []T
The append function is used to add values to an array slice. It takes number of arguments. The first argument is the array to which we wish to add the values followed by the values to add. The function then returns the final slice of array containing all the values.
Algorithm
Step 1 − Create a package main and import fmt and reflect package in the program.
Step 2 − Create a function main and in that function create two slices of type string which are to be compared with each other.
Step 3 − In the first case before adding any new element in the slice, compare the slices using reflect.DeepEqual() function with the slices as parameters.
Step 4 − In the second case add new string in the slice and compare the slices using reflect.DeepEqual() function with the slices as parameters.
Step 5 − The output will be printed using fmt.Prinln() function on the console as a Boolean value.
Example
Golang program to compare elements in two slices using built-in function
package main import ( "fmt" "reflect" ) func main() { str1 := []string{"Goa", "Gujarat"} // create slices str2 := []string{"Goa", "Gujarat"} fmt.Println("The strings are equal or not before adding any element:") fmt.Println(reflect.DeepEqual(str1, str2)) // print slice equality before adding any element str2 = append(str2, "Mumbai") fmt.Println("The strings are equal or not after adding any element:") fmt.Println(reflect.DeepEqual(str1, str2)) //printing slice equality after adding extra element }
Output
The strings are equal or not before adding any element: true The strings are equal or not after adding any element: false
Conclusion
In this tutorial, of comparing slices, we used two methods to execute the program. In the first method we used custom function with some conditions and in the second method we used a built-in function named reflect.DeepEqual() function.
- Related Articles
- How to compare two slices of bytes in Golang?
- Golang program to merge two slices
- Golang program to calculate union of two slices
- Golang program to calculate difference between two slices
- How to compare slices, structs and maps in Golang?
- Golang Program to Calculate the intersection of two Slices
- Golang program to check if two slices are equal
- How to concatenate two slices in Golang?
- Golang program to compare two strings
- Golang program to calculate the symmetric difference between two slices
- Golang program to compare two strings lexicographically
- Python Program to compare elements in two dictionaries
- Golang Program to compare two strings by ignoring case
- How to compare two strings in Golang?
- Golang Program to find the common elements from two arrays
