Form Smallest Number From Two Digit Arrays
Here’s a Python solution:


The first step is to check for common numbers in both arrays (intersection). If there is a common number, we return the smallest one, as it will be the smallest number that contains at least one digit from each array.
If there is no common number, then the smallest number that contains at least one digit from each array will be a twodigit number. This number is formed by the smallest number from each array, with the smaller one in the tens place and the larger one in the ones place. That’s why we multiply the smaller one by 10 and add the larger one.
Remember that the constraints in the problem state that all digits in each array are unique and range from 1 to 9. This means we don’t have to worry about zero as a potential digit.
Q&A
What’s wrong with my code?


Your code has several issues:
You are returning a map
myMap
when the function is expected to return anint
. This will cause a type mismatch error.In the problem statement, you’re asked to find the smallest number that contains at least one digit from each array. However, in your code, you’re simply sorting each array and storing the smallest element in a map, which doesn’t serve the purpose of the problem.
The use of
map
in your code is not necessary. Your problem can be solved by using a simple comparison of elements in both arrays.
You need to restructure your code to find common elements in both arrays and return the smallest common element. If no common elements exist, you should return a twodigit number formed by the smallest numbers from both arrays.
Here’s a corrected version of your code:


In this version, we first sort both arrays. Then we compare each element in both arrays to find the smallest common element. If there is no common element, we return a twodigit number composed of the smallest elements from both arrays.