Menu Close

Go – Program to find Intersection of Two Arrays

Here, this tutorial will help you to understand about program to find Intersection of Two Arrays with algorithm.

You are given two integer array let’s say array1 and array2. You have to return an array of their intersection. It means, each element in the result must be unique. You can return result in any order.

You can also get same problem in leetcode-349 which is similar to program to find Intersection of Two Arrays.

Example 1:

Input: 
array1 = [2, 5, 5, 6, 7], 
array2 = [5, 2]

Output: [2, 5]
Output can also be [5, 2]

Example 2:

Input:
array1 = [5, 10, 6], 
array2 = [10, 5, 10, 9, 5, 5]

Output: [10, 5]
Output can also be [5, 10]

Algorithm to find Intersection of Two Arrays:

  • Create map and store the first array value to map
    • In map, Use array value as key and bool / int as value
  • Iterate over the second array
    • If the value of second array is present in map then add that value to result array with given below condition:
      • Before inserting to result array, You will have to make sure that there should not be any duplicate element.
  • Return the result array

Code in go – 1:

package main
import "fmt"

func intersection(nums1 []int, nums2 []int) []int {
    // Create a map
    m := make(map[int]int)
    
    // Store the nums1 array value to map
    for i := 0; i < len(nums1); i++ {
        m[nums1[i]] = 1;
    }

    for i := 0; i < len(nums2); i++ {
        if _, ok := m[nums2[i]]; ok {
            m[nums2[i]] += 1;
        }
    }
    arr := []int{}
    
    for key, val := range(m) {
        if val > 1 {
            arr = append(arr, key)
        }
    }
    return arr
}

func main() {
    arr1 := []int{2, 5, 5, 6, 7}
    arr2 := []int{5, 2}
    
    intersection_array := intersection(arr1, arr2)
    
    for _, val := range(intersection_array) {
        fmt.Println(val)
    }
}

Code in go – 2:

package main
import "fmt"

func intersection(nums1 []int, nums2 []int) []int {
    m := map[int]bool{}
    result := []int{}
    
    for _,num := range nums1{
        m[num] = true
    }
    
    for _, num := range nums2{
        if(m[num] == true){
            result = append(result, num)
            m[num] = false
        }
    } 
    return result    
}

func main() {
    arr1 := []int{5, 10, 6}
    arr2 := []int{10, 5, 10, 9, 5, 5}
    
    intersection_array := intersection(arr1, arr2)
    
    for _, val := range(intersection_array) {
        fmt.Println(val)
    }
}

Code in C++:

class Solution {
public:
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
        map<int, int> m;
        vector<int> v;
        
        for(auto it : nums1) {
            m[it] = 1;
        }
        for(auto it : nums2) {
            if (m.find(it) != m.end())
                m[it] += 1;
        }
        for(auto it : m) {
            if(it.second > 1)
                v.push_back(it.first);
        }
        return v;
    }
};

To learn more about C++ pls refer given below link:

https://www.techieindoor.com/category/golang/

References:

https://www.cplusplus.com/

Posted in C++, golang program, Leetcode

Leave a Reply

Your email address will not be published.

Contact Us