Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Find different index in arrays compare

I’m trying to learn a bit Java with tutorials and currently I’m struggling with piece of code where I should find on which index is difference between arrays (if there is difference at all)

My code

Scanner scanner = new Scanner(System.in);

int[] arrOne = Arrays.stream(scanner.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
int[] arrTwo = Arrays.stream(scanner.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
int sumArrOne = 0;
int index = 0;
boolean diff = false;

for (int k : arrOne) {
   if (Arrays.equals(arrOne, arrTwo)) {
       sumArrOne += k;
   } else {
       for (int i : arrTwo) {
           if (k != i) {
              index = i;
              diff = true;
               break;
           }
       }
   }
}
if (diff) {
    System.out.println("Found difference at " + index + " index.");
} else {
    System.out.println("Sum: " + sumArrOne);
}  

So, if arrays are identical I’m sum array elements in arrOne. If they are not identical -> must show at which index they are not.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

With this code when I input

1 2 3 4 5
1 2 4 3 5

I should get that difference is at index 2 instead I’ve got index 1.

I’m not quite sure why and would be glad if someone point me out where is my mistake.

>Solution :

I updated your code. Looks like you’re misunderstanding the concept of indexes yet.

Use one common index to check with in both arrays, in my example it’s simply called i:

import java.util.Arrays;
import java.util.Scanner;

public class BadArray {

    static private final int INVALID_INDEX = Integer.MIN_VALUE;

    public static void main(final String[] args) {
        try (final Scanner scanner = new Scanner(System.in);) {


            final int[] arrOne = Arrays.stream(scanner.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
            final int[] arrTwo = Arrays.stream(scanner.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
            int sumArrOne = 0;
            int diffIndex = INVALID_INDEX;

            final int minLen = Math.min(arrOne.length, arrTwo.length);
            for (int i = 0; i < minLen; i++) {
                sumArrOne += arrOne[i];
                if (arrOne[i] != arrTwo[i]) {
                    diffIndex = i;
                    break;
                }
            }

            if (diffIndex != INVALID_INDEX) {
                System.out.println("Found difference at " + diffIndex + " index.");
            } else if (arrOne.length != arrTwo.length) {
                System.out.println("Arrays are equal but have different length!");
            } else {
                System.out.println("Sum: " + sumArrOne);
            }
        }
    }

}

I also put the scanner into a try-resource-catch to handle resource releasing properly.

Note you could also do the array lengths comparison right at the start if different array lengths play a more crucial role.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading