Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Measuring overlap between arrays

Tags:

java

arrays

Given several arrays in Java (i'll be looping through keys currently stored in a HashMap), I want to be able to identify (based on the boolean[] keys currently stored) which indices are true in all of them, and which are false.

Example:

{true, true,false}
{true,false,false}
{true,false, false}

would yield index 0 as having all true values, and index 2 as having all false values.

One idea I have is to convert the boolean array to an array of ints, summing the values, and if the summed array index = numArrays, then it is True in all of them. Similarly, if the summed array index is 0, it is false in all of them.

Am not sure how to go about doing this in an efficient way (in java), letalone if this is a good method to achieve my desired result.

Is there a way to convert the array of booleans to ints? If not, is the easiest alternative to instantiate a new array of ints, and loop through the boolean array to populate the new array?

like image 297
jfalkson Avatar asked Oct 19 '22 01:10

jfalkson


2 Answers

They are booleans, so I really don't know why you are thinking of converting them to integers and summing them up: it would be really easy but definitely useless.

So. I assume you don't have them in a matrix (I actually assume you don't know what a matrix is, but please don't get offended by this), you have them in separate arrays and you exactly know how many arrays you have (let's say 3). I also assume they all have the same length.

The idea is: for each available position, we check the three elements occupying that position in the three arrays and see if they are all true. What can we do with the result from this comparison? We can print it to screen, put it in another array, or whatever we fancy doing.

Let's start with the three arrays:

boolean[] foo = {true, true, false};
boolean[] bar = {true, false, false};
boolean[] baz = {true, false, false};

Ok. We can now create a fourth array to store our results.

boolean[] qux = new boolean[3];

or, even better:

boolean[] qux = new boolean[foo.length];

Let's start building our C-styled loop:

for (i = 0; i < foo.length; i++) {
    ...
}

If you are working with arrays, you probably already know how a for loop works: first statement will be executed as if it were written before the whole block of code within curly brackets; second statement gets checked as first instruction every time the block is executed and in case it fails the loop stops; third statement will be executed as last instruction every time the block gets executed.
And you probably already know that if an array has length n, the indexes for its elements will be
0, [...], n-1.
In case you didn't know these two things... Well, you're welcome.

So, what did I say we want to do with every available position? We want to compare the three elements in that position. So the for loop becomes:

for (int i = 0; i < foo.length; i++) {

    if (foo[i] && bar[i] && baz[i]) {
        ...
    }

}

What we just added is this check: if the element at position i in array foo is a true, AND ALSO the element at position i in array bar is a true, AND ALSO the element at position i in array baz is a true... We do something.

We can for instance put the result in qux:

for (int i = 0; i < foo.length; i++) {

    if (foo[i] && bar[i] && baz[i]) {

        qux[i] = true;

    } else {

        qux[i] = false;

    }

}

Note that the comparison itself evaluates to a boolean, and when its value is true we store a true; when false we store a false. So we can also write:

for (int i = 0; i < foo.length; i++) {

    qux[i] = (foo[i] && bar[i] && baz[i]);

}

Grrrrreat, we achieved what we wanted to achieve! Do you also want to display such result? Let me introduce you the enhanced for loop:

for (boolean val : qux) {
    ...
}

Basically, being qux an array of booleans, it is a "container" of boolean values. This for statement says: for each of the elements making up qux... Do something.

So, let's use it to print these elements:

for (boolean val : qux) {

    System.out.println(val);

}

Method println(), in object out, static member of class System (don't worry if you don't understand all these terms, you will learn their meaning if you continue studying Java) displays on screen something. Technically, this "something" should be a String, but since Java knows how to make strings out of booleans, we don't have to worry too much when passing val to it.

Want to do everything within the first for and also print in a pretty way? Easy as pie:

for (int i = 0; i < foo.length; i++) {

    qux[i] = (foo[i] && bar[i] && baz[i]);

    System.out.println("At position " + i + " we have " + qux[i]);

}

So, how do we now check for all-false positions? We can use the bang (!), whose meaning is: take this boolean and consider its negation (true if it is false and false if it is true).
Using it, we can express this check: if the element at position i in array foo is a false, AND ALSO the element at position i in array bar is a false, AND ALSO the element at position i in array baz is a false... We do something.

So we would need a fifth array:

boolean[] norf = new boolean[foo.length];

And we would change our loop to

for (int i = 0; i < foo.length; i++) {

    qux[i] = (foo[i] && bar[i] && baz[i]);

    if (qux[i]) {

            System.out.println("At position " + i + " they are all true.");

    }

    norf[i] = (!foo[i] && !bar[i] && !baz[i]);

    if (norf[i]) {

            System.out.println("At position " + i + " they are all false.");

    }

}

Hope this was useful.

like image 139
pimple Avatar answered Oct 21 '22 21:10

pimple


Assuming all arrays will have same number of elements you can try this:

public static void main(String[] args) {
    boolean[] array1 = new boolean[] {true,true,false};
    boolean[] array2 = new boolean[] {true,false,false};
    boolean[] array3 = new boolean[] {true,false,false};
    boolean[] array4 = new boolean[] {true,false,false};

    compareAndPrintCommonIndicesForArrays(array1, array2, array3);
}

public static boolean compareAndPrintCommonIndicesForArrays(boolean[] ...arrays) {
    boolean result =true;
    boolean itemToCompare;
    int elementsToCompare = arrays[0].length;

    for(int itemIndex=0; itemIndex<elementsToCompare; itemIndex++) {
        result = true;
        itemToCompare = arrays[0][itemIndex];
        for(int arrayIndex = 0; arrayIndex<arrays.length;arrayIndex++) {
            if(itemToCompare != arrays[arrayIndex][itemIndex]) {
                result = false;
            }
        }
        if(result) System.out.println(itemIndex);
    }
    return result;
}

It prints:

0 2

The code will traverse all indices and for every index it will compare elements from all the array at that index. If they are all same then the index is printed.

like image 28
akhil_mittal Avatar answered Oct 21 '22 21:10

akhil_mittal