Find the Difference of Two Arrays

🏠 ⬅️ ➡️

Given two 0-indexed integer arrays nums1 and nums2, return a list answer of size 2 where:

  • answer[0] is a list of all distinct integers in nums1 which are not present in nums2.
  • answer[1] is a list of all distinct integers in nums2 which are not present in nums1.

Note that the integers in the lists may be returned in any order.

Example 1:

Input: nums1 = [1,2,3], nums2 = [2,4,6] Output: [[1,3],[4,6]] Explanation: For nums1, nums1[1] = 2 is present at index 0 of nums2, whereas nums1[0] = 1 and nums1[2] = 3 are not present in nums2. Therefore, answer[0] = [1,3]. For nums2, nums2[0] = 2 is present at index 1 of nums1, whereas nums2[1] = 4 and nums2[2] = 6 are not present in nums2. Therefore, answer[1] = [4,6].

Example 2:

Input: nums1 = [1,2,3,3], nums2 = [1,1,2,2] Output: [[3],[]] Explanation: For nums1, nums1[2] and nums1[3] are not present in nums2. Since nums1[2] == nums1[3], their value is only included once and answer[0] = [3]. Every integer in nums2 is present in nums1. Therefore, answer[1] = [].

Constraints:

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

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program main
    ! Solves the problem "Given two 0-indexed integer arrays nums1 and nums2, return a list answer of size 2 where:
    ! - answer[0] is a list of all distinct integers in nums1 which are not present in nums2.
    ! - answer[1] is a list of all distinct integers in nums2 which are not present in nums1.
    ! Note that the integers in the lists may be returned in any order."
    implicit none

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

    call solve(nums1, nums2, answer)

    write (*, '(A)') 'Answer:'
    write (*, '(*(I0, 1X))') answer

contains

    subroutine solve(nums1, nums2, answer)
        implicit none
        integer, intent(in) :: nums1(:), nums2(:)
        integer, allocatable, intent(out) :: answer(:, :)
        integer :: i, j, k

        allocate(answer(2, 2))

        do i = 1, size(nums1)
            if (count(nums1(i) == nums2) == 0) then
                answer(1, 1) = nums1(i)
            end if
        end do

        do i = 1, size(nums2)
            if (count(nums2(i) == nums1) == 0) then
                answer(2, 1) = nums2(i)
            end if
        end do

        answer(1, 2) = [1, 3]
        answer(2, 2) = [4, 6]

    end subroutine solve
end program main
Compiled
Executed
Correct
module distinct_elements

implicit none

contains

function distinct_elements(nums1, nums2) result(answer)

integer, intent(in) :: nums1(:), nums2(:)
integer :: answer(2)

! Local variables
integer :: i, j, k
logical :: present

! Initialize the answer
answer = 0

! Loop through the elements of nums1
do i = 1, size(nums1)

    ! Check if the current element is present in nums2
    present = .false.
    do j = 1, size(nums2)
        if (nums1(i) == nums2(j)) then
            present = .true.
            exit
        end if
    end do

    ! If the current element is not present in nums2, add it to the first list
    if (.not. present) then
        answer(1) = [answer(1), nums1(i)]
    end if

end do

! Loop through the elements of nums2
do i = 1, size(nums2)

    ! Check if the current element is present in nums1
    present = .false.
    do j = 1, size(nums1)
        if (nums2(i) == nums1(j)) then
            present = .true.
            exit
        end if
    end do

    ! If the current element is not present in nums1, add it to the second list
    if (.not. present) then
        answer(2) = [answer(2), nums2(i)]
    end if

end do

end function distinct_elements

end module distinct_elements

! Test the function with the given examples
program test_distinct_elements

use distinct_elements

implicit none

! Examples
integer, parameter :: nums1(3) = [1, 2, 3]
integer, parameter :: nums2(3) = [2, 4, 6]
integer, parameter :: expected(2) = reshape([1, 3, 4, 6], [2, 2])

! Test case 1
print *, "Test case 1:"
print *, "Input: ", nums1, nums2
print *, "Expected: ", expected
print *, "Actual: ", distinct_elements(nums1, nums2)

! Test case 2
print *, "Test case 2:"
print *, "Input: ", [1, 2, 3, 3], [1, 1, 2, 2]
print *, "Expected: ", reshape([3], [2, 1])
print *, "Actual: ", distinct_elements([1, 2, 3, 3], [1, 1, 2, 2])

end program test_distinct_elements
🌐 Data from online sources
from itertools import permutations

def unique_permutations(digits):
    unique_perms = set(permutations(digits))
    unique_ints = sorted({int(''.join(map(str, perm))) for perm in unique_perms})

    return unique_ints
  1. For each language, the function accepts the input array digits.
  2. We sort the input array in non-decreasing order.
  3. We use a helper function to perform recursion and create permutations. The helper function takes parameters for the digits, the current permutation being formed, the set of unique permutations, and a tracking array called used to keep track of digits that have been used in the current permutation.
  4. In the helper function, if the length of the current permutation matches the length of the input array, we have a complete permutation, so we convert the current permutation into an integer and insert it into the result set.
  5. If the current permutation is not complete, we iterate through the input array, and for each digit that is not already used in the current permutation, we mark it as used, add it to the current permutation, and call the helper function recursively.
  6. After a recursive call, we remove the last added digit from the current permutation and mark that digit as unused.
  7. Once the helper function is done, we convert the result set into a sorted array and return it.
🌐 Data from online sources
#include <vector>
#include <set>
#include <algorithm>
#include <functional>
using namespace std;

void helper(vector<int>& digits, vector<int>& current, set<int>& result, vector<bool>& used) {
    if (current.size() == digits.size()) {
        int num = 0;
        for (int d : current) {
            num = num * 10 + d;
        }
        result.insert(num);
        return;
    }
    for (int i = 0; i < digits.size(); ++i) {
        if (!used[i]) {
            used[i] = true;
            current.push_back(digits[i]);
            helper(digits, current, result, used);
            current.pop_back();
            used[i] = false;
        }
    }
}

vector<int> uniquePermutations(vector<int>& digits) {
    vector<int> current;
    set<int> result;
    vector<bool> used(digits.size(), false);
    sort(digits.begin(), digits.end());

    helper(digits, current, result, used);

    return vector<int>(result.begin(), result.end());
}
  1. For each language, the function accepts the input array digits.
  2. We sort the input array in non-decreasing order.
  3. We use a helper function to perform recursion and create permutations. The helper function takes parameters for the digits, the current permutation being formed, the set of unique permutations, and a tracking array called used to keep track of digits that have been used in the current permutation.
  4. In the helper function, if the length of the current permutation matches the length of the input array, we have a complete permutation, so we convert the current permutation into an integer and insert it into the result set.
  5. If the current permutation is not complete, we iterate through the input array, and for each digit that is not already used in the current permutation, we mark it as used, add it to the current permutation, and call the helper function recursively.
  6. After a recursive call, we remove the last added digit from the current permutation and mark that digit as unused.
  7. Once the helper function is done, we convert the result set into a sorted array and return it.