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
Golang Program To Find Common Array Elements
In this tutorial, we will see to write a go language program to find common elements in two arrays.
Find Common Array Elements Using User-Defined Function
The following code illustrates how we can find the common elements in two different arrays of strings.
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 an 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 ? If the condition is true then we need to store that element in the empty array created above and flip the Boolean value of the bucket map.
Step 6 ? Repeat the above process until whole arrays are checked and return the final value.
Step 7 ? Start the main() function.
Step 8 ? Initialize two arrays of strings and store values to them and 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 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
package main
import "fmt"
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{"one", "two", "three", "four"}
fmt.Println("The first array entered is:", arr1)
arr2 := []string{"two", "four"}
fmt.Println("The second array entered is:", arr2)
result := intersection(arr1, arr2)
fmt.Println("The common elements of the above two arrays are:", result)
}
Output
The first array entered is: [one two three four] The second array entered is: [two four] The common elements of the above two arrays are: [two four]
Find Common Array Elements In An Array Of Integers Using External Functions
In this example, we will write a go language program to find common integer array elements using user-defined functions.
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
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{1, 2, 3, 4, 5}
fmt.Println("The first array entered is:", arr1)
arr2 := []int{4, 5, 6, 7}
fmt.Println("The second array entered is:", arr2)
result := intersection(arr1, arr2)
fmt.Println("The common elements of the above two arrays are:", result)
}
Output
The first array entered is: [1 2 3 4 5] The second array entered is: [4 5 6 7] The common elements of the above two arrays are: [4 5]
Conclusion
We have successfully compiled and executed a go language program to find the common elements of two arrays along with the examples.