I would fill in the smaller array to the bigger dimensions with null values (or with NaN), convert to 1D and truncate/strip the unnecessary nulls :
array_1 = [1, 2, null, null, 5, 6]
array_2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
then compare the 1D arrays, while skipping the null values - this would be O(n*m)
in the worst case (such as [1,1,1,2]
vs [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
), and it would be O(n)
in the best case (if every number in the bigger array was different)
Edit: more logic is needed to ensure comparison only within the complete rows of the bigger array, not across rows...
I guess you could convert the arrays to dictionaries of positions and figure out a bit more complicated and faster algorithm if you need to do multiple comparisons...
You could also rotate the smaller array if needed, e.g.:
array_1_270 = [6, 2, null, null, 1, 5]