Find the Difference of Two Arrays

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 sets
        Set<Integer> st1 = new HashSet<Integer>();
        Set<Integer> st2 = new HashSet<Integer>();

        // add all the elements of first arrays into the set 1
        for (int i = 0; i < nums1.length; i++) {
            st1.add(nums1[i]);
        }

        // add all the elements of second arrays into the set 2
        for (int i = 0; i < nums2.length; i++) {
            st2.add(nums2[i]);
        }

        // remove the element which present in the set 1 and from array 2
        for (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 1
        for (int i = 0; i < nums1.length; i++) {
            if (st2.contains(nums1[i]))
                st2.remove(nums1[i]);
        }

        // creating the lists
        List<Integer> answer1 = new ArrayList<Integer>();

        List<Integer> answer2 = new ArrayList<Integer>();

        Iterator<Integer> value1 = st1.iterator();
        // add the set 1 element into list 1
        while (value1.hasNext()) {
            answer1.add(value1.next());
        }

        Iterator<Integer> value2 = st2.iterator();

        // add the set 1 element into list 1
        while (value2.hasNext()) {
            answer2.add(value2.next());
        }

        List<List<Integer>> result = new ArrayList<List<Integer>>();

        // add both the list into one list
        result.add(answer1);
        result.add(answer2);

        // return the final list
        return 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 1
    for (int i = 0; i < nums1.size(); i++)
    {

        st1.insert(nums1[i]);
    }

    // insert the second array elements into set 2
    for (int i = 0; i < nums2.size(); i++)
    {
        st2.insert(nums2[i]);
    }

    // remove the element from set 1 which present in second array
    for (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 array
    for (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]]

URLStreamHandlerFactory interface in Java

java.net.URLStreamHandlerFactory

This interface defines a factory for URL stream protocol handlers.

A URL stream handler factory is used as specified in the URL constructor.

Declaration

public interface URLStreamHandlerFactory {
   
    URLStreamHandler createURLStreamHandler(String protocol);
}


Methods

1. URLStreamHandlerFactory.createURLStreamHandler(String protocol)

URLStreamHandler java.net.URLStreamHandlerFactory.createURLStreamHandler(String protocol)

This method takes one argument. This method creates a new URLStreamHandler instance with the specified protocol.

Parameters: One parameter is required for this method.

protocol: the protocol ("ftp", "http", "nntp", etc.).

Returns: a URLStreamHandler for the specific protocol, or null if this factory cannot create a handler for the specific protocol.

Exceptions: NA

URLPermission class in Java

java.net.URLPermission

Represents permission to access a resource or set of resources defined by a given url, and for a given set of user-set table request methods and request headers. The name of the permission is the url string. The actions string is a concatenation of the request methods and headers. The range of methods and header names is not restricted by this class.

Some methods of URLPermission class

URLPermission(String)This method creates a URLPermission with the given url string and unrestricted methods and requests headers by invoking the two-argument constructor as follows: URLPermission(url, "*:*")

URLPermission(String, String)This method creates a new URL permission from a url string and permits the given request methods and user-settable request headers.

equals(Object)his method returns true if, this.getActions().equals(p.getActions())and p's url equals this's url. Returns false otherwise.

getActions()This method returns the normalized method list and requests a header list, in the form: "method-names: header-names".

hashCode()This method returns a hashcode calculated from the hashcode of the actions String and the url string.

implies(Permission)This method checks if this URLPermission implies some of the permissions.

URLPermission implies(Permission) in Java

implies(Permission): This method is available in the java.net.URLPermission class of Java.

Syntax:

boolean java.net.URLPermission.implies(Permission p)

This method takes one argument. This method checks if this URLPermission implies the given permission.

Specifically, the following checks are done in the following sequence:

1. if 'p' is not an instance of URLPermission return false.

2. if any of p's methods are not in this's method list and if this's method list is not equal to "*", then return false.

3. if any of p's headers are not in this's request header list, and if this's request header list is not equal to "*", then return false.

4. if this's url scheme is not equal to p's url scheme return false.

4. if the scheme-specific part of this's url is '*' return true.

5. if the set of hosts defined by p's url host range is not a subset of this's url host range then return false.

6. if the port range defined by p's url is not a subset of the port range defined by this's url then return false.

7. if the path or paths specified by p's url are contained in the set of paths specified by this's url, then return true

8. otherwise, return false

Parameters: One parameter is required for this method.

p: the permission to check against.

Returns: true if the specified permission is implied by this object, false if not.

Exceptions: NA

Approach

Java

package com.URLPermission;

import java.net.URLPermission;

public class URLPermissionimplies {
    public static void main(String[] args) {

        String url = "https://beingcodeexpert.blogspot.com/";
        String actions = "read";
        URLPermission urlPermission = new URLPermission(url, actions);

        System.out.println(urlPermission.implies(urlPermission));
    }
}

Output:

true


Some other methods of URLPermission class

URLPermission(String)This method creates a URLPermission with the given url string and unrestricted methods and requests headers by invoking the two-argument constructor as follows: URLPermission(url, "*:*")

URLPermission(String, String)This method creates a new URL permission from a url string and permits the given request methods and user-settable request headers.

equals(Object)his method returns true if, this.getActions().equals(p.getActions())and p's url equals this's url. Returns false otherwise.

getActions()This method returns the normalized method list and requests a header list, in the form: "method-names: header-names".

hashCode()This method returns a hashcode calculated from the hashcode of the actions String and the url string.

implies(Permission)This method checks if this URLPermission implies some of the permissions.