Limited access

Which of the following methods will correctly return the count of all of the different elements from array1 and array2, when comparing the elements at the same positions in each array?

If one array ends before the other array, the remaining elements are considered to all be different.

For example, if array1 = {5, 6, 7, 8, 9} and array2 = {5, 7, 7, 9, 9, 1, 2, 3}, then the method should return 5 since the elements at positions 1 and 3 from each array are not equal, and the last three elements from array2 do not have any matching elements from array1.

A
public static int countDiffElements(int[] array1, int[] array2)
{
int count=0;
for (int i=0; i<array1.length; i++)
{
if (array1[i] != array2[i]  && i<array2.length)
count++;
}
if (array2.length>array1.length)
count += array2.length - array1.length;
return count;
}

B
public static int countDiffElements(int[] array1, int[] array2)
{
int count=0;
for (int i=0; i<array1.length && i<array2.length; i++)
{
if (array1[i] != array2[i])
count++;
}
return count;
}

C
public static int countDiffElements(int[] array1, int[] array2)
{
int count=0;
for (int i=0; i<array1.length; i++)
{
if (i>=array2.length || array1[i] != array2[i])
count++;
}
if (array2.length>array1.length)
count += array2.length - array1.length;
return count;
}

D
public static int countDiffElements(int[] array1, int[] array2)
{
int count=0;
for (int i=0; i<array1.length; i++)
{
if (i>=array2.length || array1[i] != array2[i])
count++;
}
if (array1.length>array2.length)
count += array1.length – array2.length;
return count;
}

E
public static int countDiffElements(int[] array1, int[] array2)
{
int count=0;
for (int i=0; i<array1.length; i++)
{
if (i>=array2.length)
return count;
else
if (array1[i] != array2[i])
count++;
}
if (array1.length>array2.length)
count ++;
return count;
}

Select an assignment template