# To compare the contents of two arrays, you must compare the elements of the two arrays.

25 views

## Problem :

My function accepts the two arrays of integers and returns true if below conditions are met

1.      Arrays have the same length and

2.      Each m.element should be less than each n.element of the same index

All my test cases are passing except when int[] m = {1, 2, 3} and int[] n = {4, 5, 1}.

It is returning true even if m > n. Here digitDifference check is not working correctly but I am not getting any error.

public static boolean ArrayLess(int[] m, int[] n) {

int j = 0;

boolean sameLength = (m.length == n.length);

boolean digitDifference = (m[j] < n[j]);

for (j = 0; j <= m.length - 1; j++) {}

return (sameLength && digitDifference);

}

## Solution :

I guess your for loop does nothing, so I think you are only comparing the elements in the first index of your arrays.

Your code must look something like below :

`public static boolean ArrayLess (int[] m, int[] n) {`
`boolean sameLength = (m.length == n.length);`
`if (!sameLength)`
`   return false;`
`    boolean digitDifference = true;`
`    for (int j = 0; j <= m.length - 1 && digitDifference;j++) {`
`        digitDifference = (m[j] < n[j]);`
`    }`
`    return digitDifference;`
`}`

Now the newly updated for loop in above code compares each pair of elements having the same index, and terminates if it finds a pair that against your requirement that (m[j] < n[j]).

## Related questions

1 vote
15 views
Problem: Hello good souls, var myarr1 = [1, 2, 3, 4, 5]; var myarr2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; I have two simple arrays like above. I want to sort out the common values between those two arrays. So can you find common elements in two arrays javascript for me? Please keep the program as simple as possible that I can understand easily. Thanks for your effort and valuable time buddy.
1 vote
150 views
Problem : I am beginner to numpy. I am trying to execute my code but I am facing below error. &ldquo;Valueerror: all the input arrays must have same number of dimensions&rdquo;. I want to have my all the arrays with a same shape. I am trying to fix above error from past couple of days but still unable to fix it. I am looking for some help in fixing above error.
1 vote