socrlax24
socrlax24

Reputation: 111

Compare two int arrays and print the missing numbers

I'm trying to compare two int arrays, where array 1 is the standard (1...n) and array 2 is random numbers within the range of (1...n). The numbers that are missing from array 2 need to be printed out. So far, I've managed the following, but I can't seem to figure out how to use the boolean array to my benefit.

import java.util.Scanner;

public class findLeaves {
    private boolean[] id;
    private String[] surface;
    private int[] head;

    public boolean[] inputId() {
        System.out.println("Input number of IDs");
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        id = new boolean[n];
        return this.id;
    }

    public int[] inputHead() {
        System.out.println("Input each head value.");
        head = new int[id.length];
        for (int i = 0; i < id.length; i++){
            Scanner scan = new Scanner(System.in);
            head[i] = scan.nextInt();
        }
        return this.head;
    }

    public boolean Leaves() {

        for (int i = 0; i < head.length; i++);
        for (int j = 0; j < id.length; j++) {
            if (!id[j]) System.out.print(j + ",");
        }
        return true;
    }

    public static void main(String[] args) {
        findLeaves x = new findLeaves();
        x.inputId();
        x.inputHead();
        x.Leaves();
    }
}

Currently, Leaves() is just printing out:

0,1,2,3,4,5,6,7,8,

Does anyone know of a way that this can be accomplished? I'm relatively new to Java, and haven't been able to find anything googling or here that solves my problem. Thanks in advance!

Edit:

When I update Leaves to be:

public boolean Leaves() {

    for (int i = 0; i < head.length; i++) id[head[i]] = true;
    for (int j = 0; j < id.length; j++) {
        if (!id[j]) System.out.print(j + ",");
    }
    return true;
}

I get an error of:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 9
at findLeaves.Leaves(findLeaves.java:28)
at findLeaves.main(findLeaves.java:39)

Upvotes: 2

Views: 1426

Answers (2)

J-Alex
J-Alex

Reputation: 7127

First of all, you have termination point here for (int i = 0; i < head.length; i++);, so you'll not even run this loop.

This is a shorter way to find missing value from original array in random:

import java.util.Arrays;

public class Main {
    private int[] original = new int[] {1, 3, 5, 7, 9}; // original array
    private int[] random = new int[] {1, 2, 3, 4, 5, 6}; // missing value from original array will be printed

    public static void main(String[] args) {
        Main m = new Main();
        Arrays.sort(m.original); // sort is required for binarySearch()
        for (int i : m.random) {
            if (Arrays.binarySearch(m.original, i) < 0)
                System.out.println(i);
        }
    }
}

With Java 8+:

import java.util.stream.IntStream;

public class Main {
    private int[] original = new int[] {1, 3, 5, 7, 9};
    private int[] random = new int[] {1, 2, 3, 4, 5, 6};

    public static void main(String[] args) {
        Main m = new Main();
        for (int i : m.random) {
            if (IntStream.of(m.original).noneMatch(value -> value == i))
                System.out.println(i);
        }
    }
}

Without any libraries:

public class Main {
    private int[] original = new int[] {1, 3, 5, 7, 9};
    private int[] random = new int[] {1, 2, 3, 4, 5, 6};

    public static void main(String[] args) {
        Main m = new Main();
        for (int i : m.random) {
            if (!contains(m.original, i))
                System.out.println(i);
        }
    }

    public static boolean contains(int[] array, int value) {
        for (int i : array)
            if (i == value)
                return true;

        return false;
    }
}

Output:

2
4
6

Upvotes: 2

Ahmed Abdelaal
Ahmed Abdelaal

Reputation: 159

change this code

public boolean Leaves() {

    for (int i = 0; i < head.length; i++) id[head[i] -1] = true;
    for (int j = 0; j < id.length; j++) {
        if (!id[j]) System.out.print((j +1)+ ",");
    }
    return true;
}

Upvotes: 0

Related Questions