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
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
temp.f95:6:34: 6 | integer, dimension(n) :: nums2 = [2, 2] | 1 Error: Different shape for array assignment at (1) on dimension 1 (4 and 2) temp.f95:21:25: 21 | intersection = [] | 1 Error: Empty array constructor at (1) is not allowed temp.f95:9:48: 9 | intersection = get_intersection(nums1, nums2) | 1 Error: Symbol ‘nums2’ at (1) has no IMPLICIT type; did you mean ‘nums1’? temp.f95:36:23: 36 | intersection = unique(intersection) | 1 Error: Function ‘unique’ at (1) has no IMPLICIT type
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
temp.f95:24:29: 24 | integer, parameter :: nums1 = [1, 2, 2, 1], nums2 = [2, 2] | 1 Error: Incompatible ranks 0 and 1 in assignment at (1) temp.f95:25:31: 25 | integer :: intersection(size(nums1)) | 1 Error: Symbol ‘nums1’ is used before it is typed at (1) temp.f95:27:15: 27 | intersection = intersection_array(nums1, nums2) | 1 Error: ‘intersection’ at (1) is not a variable temp.f95:28:53: 28 | write (*, '(A, I0)') 'Intersection: ', intersection | 1 Error: Symbol at (1) is not appropriate for an expression
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.
#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.