
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
Find Intersection of Two Sorted Arrays Using Two Pointer Approach in Go
In this Go language article, we will write programs to find intersection of two arrays using two pointer approach. Intersection means similarity or most common and intersection of two arrays refers to the similar elements between two arrays.
Syntax
func make ([] type, size, capacity)
The make function in go language is used to create an array/map it accepts the type of variable to be created, its size and capacity as arguments
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.
func range(variable)
The range function is used to iterate over any data type. To use this we first have to write the range keyword followed by the data type to which we want to iterate and as a result the loop will iterate till the last element of the variable.
sort.Ints()
This function belongs to the sort package and it is used to sort the slice/array containing integer values.
Using Two Pointer Approach in the if-else Conditionals
In this method, we will write a Golang program to find intersection of two arrays using if conditional which is used to compare the array elements and add the intersection points in the slice to print the Output.
Algorithm
Step1 ? Import the fmt, main and sort package in the program where fmt helps in the formatting of the input and Output, the main ensures that the program should be an executable program and sort is used to sort the array
Step2 ? Create a function named intersect with two input parameters array1 and the array2
Step3 ? Sort the two arrays using the Ints method from the sort package
Step4 ? Then, initialize variable i and j with 0, these variables refer to the index of array
Step5 ? In this step, create an empty intersection slice to store the intersection points of the two arrays
Step6 ? Iterate through the array1 and array2 and check if the elements are same, append the elements in the intersection slice and increment i and j variable.
Step7 ? If the first array element is less than second array element increment I variable otherwise increment j variable
Step8 ? Finally, return the slice to the external function created and in the main the Output will be received and printed on the console using Println function from the fmt package where ln means new line
Example
The following golang program explains how to find intersection of two arrays using two pointer approach in the if-else conditionals
package main import ( "fmt" "sort" ) func intersect(array1 []int, array2 []int) []int { sort.Ints(array1) sort.Ints(array2) i := 0 j := 0 var intersection []int for i < len(array1) && j < len(array2) { // If the elements are the same, add it to the intersection slice if array1[i] == array2[j] { intersection = append(intersection, array1[i]) i++ j++ } else if array1[i] < array2[j] { i++ } else { j++ } } return intersection } func main() { array1 := []int{1, 2, 2, 3} array2 := []int{2, 2} intersection_points := intersect(array1, array2) fmt.Println("The intersection points of the two arrays are:") fmt.Println(intersection_points) }
Output
The intersection points of the two arrays are: [2 2]
Using Two Pointer Approach in Frequency Map
In this method, we will write a Golang program to find intersection of two arrays using frequency map will be used to count the occurrence of each element and store inside the map.
Algorithm
Step1 ? Import the fmt and main package in the program where fmt helps in the formatting of the input and Output, the main ensures that the program should be an executable program.
Step2 ? Create a function intersect with two inputs array1 and array2 and in that function create a frequency map using make function which is a built-in function in Go
Step3 ? Iterate the array1 and count the occurrence of elements in the array and store in the map
Step4 ? In this step, create an intersection slice to store the intersection points of the two arrays
Step5 ? Next, iterate the array2 and check if the frequency of the elements is greater than 0
Step6 ? If the condition is satisfied, append the values in the intersection slice and decrement the value
Step7 ? Return the slice to the external function and in the main function print the Output using Println function from the fmt package where ln means new line
Example
The following golang program explains how to find intersection of two arrays using two pointer approach in frequency map
package main import "fmt" func intersect(array1 []int, array2 []int) []int { freq := make(map[int]int) for _, value := range array1 { freq[value]++ } var intersection []int for _, value := range array2 { if freq[value] > 0 { intersection = append(intersection, value) freq[value]-- } } return intersection } func main() { array1 := []int{1, 2, 2, 3} array2 := []int{2, 2} intersection_point := intersect(array1, array2) fmt.Println("The intersection point of two arrays is:") fmt.Println(intersection_point) }
Output
The intersection point of two arrays is: [2 2]
Conclusion
We compiled and executed the program of finding the intersection of two arrays using two pointer approach using two examples. In the first example we used conditional if-else statement and in the second example we used a frequency map to get the Output.