- 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 find the common elements from two arrays
In this tutorial, we will see to write a go language program to find common elements in two arrays. In this article we will write two programs. in the first program we will use the array of strings while in the second one we will use the array of integers.
Method 1: Using Append() Function
The following code illustrates how we can find the common elements in two different arrays of strings using an external function in go programming language. The function which we will create takes the two arrays as an argument to it and returns the final array that contains the common elements in both the arrays provided.
Algorithm
STEP 1 − import the fmt package.
STEP 2 − Define a function named intersection() that accepts the two arrays as arguments and returns the resultant array as output to the function.
STEP 3 − Create an empty array of strings called out and a map called bucket.
STEP 4 − Use for loop to iterate over the two arrays and check if the current element of one array is equal to the elements of the other.
STEP 5 − Repeat the above process until whole arrays are checked and return the final value.
STEP 6 − Start the main() function.
STEP 7 − Initialize two arrays of strings and store values to them print these two arrays on the screen.
STEP 8 − Call the intersection() function by passing the two arrays as arguments to the function and store the final result in a different variable.
STEP 9 − This variable contains the array that has the common elements.
STEP 10 − Print the result on the screen using fmt.Println() function.
Example
Golang program to find common elements from two arrays of strings using an external function
package main import "fmt" // function to get common elements 2. Golang Program to find the common elements from two arrays func intersection(arr1, arr2 []string) []string { out := []string{} bucket := map[string]bool{} for _, i := range arr1 { for _, j := range arr2 { if i == j && !bucket[i] { out = append(out, i) bucket[i] = true } } } return out } func main() { arr1 := []string{"apple", "mango", "banana", "papaya"} fmt.Println("The first array entered is:", arr1) arr2 := []string{"cherry", "papaya", "mango"} fmt.Println("The second array entered is:", arr2) result := intersection(arr1, arr2) fmt.Println() fmt.Println("The common elements of the above two arrays are:", result) }
Output
The first array entered is: [apple mango banana papaya] The second array entered is: [cherry papaya mango] The common elements of the above two arrays are: [mango papaya]
Method 2: Using Mapping Method
The following code illustrates how we can find the common elements in two different arrays of integers using an external function in go programming language. The function which we will create takes the two arrays as an argument to it and returns the final array that contains the common elements in both the arrays provided.
Algorithm
STEP 1 − import the fmt package.
STEP 2 − Define a function named intersection() that accepts the two arrays as arguments and returns the resultant array as output to the function.
STEP 3 − Create an a map called m with keys as integers and value as Booleans.
STEP 4 − Use for loop to iterate over the array and store its value to the map.
STEP 5 − Now, use another for loop to iterate over the second array if the current element of array is equal to the map then store this element in a new array.
STEP 6 − Repeat the above process until whole arrays are checked and return the final array.
STEP 7 − Start the main() function.
STEP 8 − Initialize two arrays of integers and store values to them print these two arrays on the screen.
STEP 9 − Call the intersection() function by passing the two arrays as arguments to the function and store the final result obtained in a different variable.
STEP 10 − This variable contains the array that has the common elements.
STEP 11 − Print the result on the screen using fmt.Println() function.
Example
Golang program to find common elements from two arrays of integers using external functions
package main import ( "fmt" ) // creating an Intersection function func intersection(a, b []int) (c []int) { m := make(map[int]bool) for _, item := range a { // storing value to the map m[item] = true } for _, item := range b { if _, ok := m[item]; ok { c = append(c, item) } } return c } func main() { arr1 := []int{10, 25, 34, 56, 69} fmt.Println("The first array entered is:", arr1) arr2 := []int{34, 69, 54, 44} fmt.Println("The second array entered is:", arr2) fmt.Println() result := intersection(arr1, arr2) fmt.Println("The common elements of the above two arrays are:", result) }
Output
The first array entered is: [10 25 34 56 69] The second array entered is: [34 69 54 44] The common elements of the above two arrays are: [34 69]
Conclusion
We have successfully compiled and executed a go language program to find the common elements of two arrays along with the examples. We have made two programs in this. The first program uses an array of strings while the second one uses an array of integers to implement the result.