Q:

Fastest way to get all unique numbers between two different dimension numpy arrays

I have two numpy arrays of varying dimensions, and I need to get all unique ints between them both. 
Since they are of varying dimensions, I decided to get the unique numbers for each of them, concatenate them, and then get the unique values for that.  
Initial approach
import numpy as np
import time

array1 = np.random.randint(1, high=5000, size=(512, 4) , dtype=np.int32)
array2 = np.random.randint(1, high=5000, size=(512, 64) , dtype=np.int32)

start = time.time()
np.unique( np.concatenate( [np.unique(array1), np.unique(array2)] ) )
end = time.time()
print(end-start)

Output (average out of 5):

0.011

Environment: Google Colab 
Is there a faster way?
My guess would be to perform only one np.unique operation, doing that with different sized numpy arrays is the challenge of that. 

A:

If your ints don't spread too far there is indeed a faster approach. The code below assumes for simplicity nonnegative ints 
smaller than 5000.
def use_mask():
    mask = np.zeros(5000,bool)
    mask[array1] = True
    mask[array2] = True
    return np.where(mask)

To make this a proper function you'd have to find the min and max of array1/2 combined, take the difference, fall back to something else if it is too large, otherwise subtract the min from array1/2 and proceed more or less like I did.

