Intersection of Two Arrays

🏠 ⬅️ ➡️

Given two integer arrays nums1 and nums2, return an array of their intersection. Each element in the result must be unique and you may return the result in any order.

Example 1:

Input: nums1 = [1,2,2,1], nums2 = [2,2] Output: [2]

Example 2:

Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4] Output: [9,4] Explanation: [4,9] is also accepted.

Constraints:

  • 1 <= nums1.length, nums2.length <= 1000
  • 0 <= nums1[i], nums2[i] <= 1000

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program main
    implicit none

    integer, parameter :: n = 4
    integer, dimension(n) :: nums1 = [1, 2, 2, 1]
    integer, dimension(n) :: nums2 = [2, 2]
    integer, dimension(:), allocatable :: intersection

    intersection = get_intersection(nums1, nums2)
    print '(A,*(I0,1X))', 'Intersection: ', intersection

contains

    function get_intersection(nums1, nums2) result(intersection)
        implicit none
        integer, intent(in) :: nums1(:), nums2(:)
        integer, dimension(:), allocatable :: intersection
        logical, dimension(size(nums1)) :: mask
        integer :: i, j

        intersection = []
        mask = .false.

        do i = 1, size(nums1)
            if (any(nums1(i) == nums2)) then
                mask(i) = .true.
            end if
        end do

        do i = 1, size(nums1)
            if (mask(i)) then
                intersection = [intersection, nums1(i)]
            end if
        end do

        intersection = unique(intersection)

    end function get_intersection

end program main
Compiled
Executed
Correct
module intersection
  implicit none
contains
  function intersection_array(nums1, nums2) result(intersection)
    integer, intent(in) :: nums1(:), nums2(:)
    integer :: intersection(size(nums1))
    integer :: i, j

    intersection = 0
    do i = 1, size(nums1)
      do j = 1, size(nums2)
        if (nums1(i) == nums2(j)) then
          intersection(i) = nums1(i)
          exit
        end if
      end do
    end do
  end function intersection_array
end module

program main
  use intersection
  implicit none
  integer, parameter :: nums1 = [1, 2, 2, 1], nums2 = [2, 2]
  integer :: intersection(size(nums1))

  intersection = intersection_array(nums1, nums2)
  write (*, '(A, I0)') 'Intersection: ', intersection
end program
🌐 Data from online sources
def intersection(nums1, nums2):
    set1 = set(nums1)
    result = set1.intersection(nums2)
    return list(result)

The function first creates a set set1 from the first array nums1, which eliminates duplicate elements in it. Then, the function iterates through the second array nums2. For each element in nums2, it checks whether it exists in the set1. If so, this element is added to the result set resultSet. Finally, the result set is converted to an array and returned.

🌐 Data from online sources
#include <vector>
#include <set>

std::vector<int> intersection(std::vector<int>& nums1, std::vector<int>& nums2) {
    std::set<int> set1(nums1.begin(), nums1.end());
    std::set<int> result;
    for (int num : nums2) {
        if (set1.count(num)) {
            result.insert(num);
        }
    }
    return std::vector<int>(result.begin(), result.end());
}

The function first creates a set set1 from the first array nums1, which eliminates duplicate elements in it. Then, the function iterates through the second array nums2. For each element in nums2, it checks whether it exists in the set1. If so, this element is added to the result set resultSet. Finally, the result set is converted to an array and returned.