Given two 0-indexed integer arrays nums1 and nums2, return a list answer of size 2 where:
1. answer[0] is a list of all distinct integers in nums1 which are not present in nums2.
2. answer[1] is a list of all distinct integers 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] = [].
Approach
Java
import java.util.ArrayList;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.Set;public class DiffTwoArray {public static void main(String[] args) {int nums1[] = { 1, 2, 3 }, nums2[] = { 2, 4, 6 };System.out.println(findDifference(nums1, nums2));}public static List<List<Integer>> findDifference(int[] nums1, int[] nums2) {// creating the setsSet<Integer> st1 = new HashSet<Integer>();Set<Integer> st2 = new HashSet<Integer>();// add all the elements of first arrays into the set 1for (int i = 0; i < nums1.length; i++) {st1.add(nums1[i]);}// add all the elements of second arrays into the set 2for (int i = 0; i < nums2.length; i++) {st2.add(nums2[i]);}// remove the element which present in the set 1 and from array 2for (int i = 0; i < nums2.length; i++) {if (st1.contains(nums2[i]))st1.remove(nums2[i]);}// remove the element which present in the set 2 and from array 1for (int i = 0; i < nums1.length; i++) {if (st2.contains(nums1[i]))st2.remove(nums1[i]);}// creating the listsList<Integer> answer1 = new ArrayList<Integer>();List<Integer> answer2 = new ArrayList<Integer>();Iterator<Integer> value1 = st1.iterator();// add the set 1 element into list 1while (value1.hasNext()) {answer1.add(value1.next());}Iterator<Integer> value2 = st2.iterator();// add the set 1 element into list 1while (value2.hasNext()) {answer2.add(value2.next());}List<List<Integer>> result = new ArrayList<List<Integer>>();// add both the list into one listresult.add(answer1);result.add(answer2);// return the final listreturn result;}}
C++
#include <bits/stdc++.h>using namespace std;vector<vector<int>> findDifference(vector<int> &nums1, vector<int> &nums2){set<int> st1, st2;// insert the first array elements into set 1for (int i = 0; i < nums1.size(); i++){st1.insert(nums1[i]);}// insert the second array elements into set 2for (int i = 0; i < nums2.size(); i++){st2.insert(nums2[i]);}// remove the element from set 1 which present in second arrayfor (int i = 0; i < nums2.size(); i++){if (st1.find(nums2[i]) != st1.end())st1.erase(nums2[i]);}// remove the element from set 2 which present in first arrayfor (int i = 0; i < nums1.size(); i++){if (st2.find(nums1[i]) != st2.end())st2.erase(nums1[i]);}vector<int> answer1, answer2;for (auto it = st1.begin(); it != st1.end(); ++it){answer1.push_back(*it);}for (auto it = st2.begin(); it != st2.end(); ++it){answer2.push_back(*it);}vector<vector<int>> result;result.push_back(answer1);result.push_back(answer2);return result;}int main(){vector<int> nums1 = {1, 2, 3}, nums2 = {2, 4, 6};vector<vector<int>> result = findDifference(nums1, nums2);cout << "[";for (int i = 0; i < result.size(); i++){cout << "[";for (int j = 0; j < result[i].size(); j++){cout << result[i][j];if (j != result[i].size() - 1)cout << ",";}cout << "]";if (i != result.size() - 1)cout << ",";}cout << "]";}
Output:
[[1, 3], [4, 6]]
No comments:
Post a Comment