user2253332
user2253332

Reputation: 827

Checking for equivalent rows in a 2D array?

So say that I have a 2D array like so:

[1, 2, 3]
[1, 2, 3, 3]
[2, 3, 1]

In the case of this problem, my function will return true because order and duplicates are disregarded. If I instead input a 2D array like this:

[1, 2, 3]
[2, 3, 4]
[6, 7, 1, 2]

It would return false, because none of the arrays have the same numbers and such.

Anyways, I'm trying to write a program that will find these equivalent arrays. Currently, I have something like this:

    public static boolean checkEqual(Integer[][] array){
    //get number of rows
    int numRows = array[0].length;
    HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
    for (int i = 0; i < numRows; i++){
        boolean alreadySeen = false;
        for (int j = 0; j < array[i].length; j++){
            if (map.containsKey(array[i][j])){
                //Do not add duplicates in the same row to the map
                if (!alreadySeen){
                    int count = map.get(array[i][j]);
                    count++;
                    map.put(array[i][j], count);
                    alreadySeen = true;
                }
            }
            else{
                map.put(array[i][j], 1);
            }
        }
    }
    boolean overall = false;
    for (Map.Entry<Integer, Integer> entry : map.entrySet()){
        if (entry.getValue() == numRows){
            overall = true;
        }
        else{
            return false;
        }
    }
    return overall;
}

However, this always returns False, and I have a feeling that this is a very inefficient way to go about doing this due to all the loops and checks. Would anyone happen to have any hints as to how I can do this in a more efficient manner? Thank you!

Upvotes: 0

Views: 70

Answers (2)

nmore
nmore

Reputation: 2603

Cleaned up xio4's solution:

import java.util.Set;
import java.util.HashSet;

public class Equaller {

    public static boolean checkEqual(Integer[][] array) {
        if (array.length > 0) {
            Set<Integer> firstSet = new HashSet<Integer>(array[0].length);
            // fill first set
            for (Integer i : array[0]) {
                firstSet.add(i);
            }
            // compare 2D array with first set
            for (Integer[] intArray : array) {
                Set<Integer> secondSet = new HashSet<Integer>(firstSet.size());
                for (Integer i : intArray) {
                    if (!firstSet.contains(i)) {
                        return false;
                    }
                    secondSet.add(i);
                }
                if (firstSet.size() != secondSet.size()) {
                    return false;
                }
            }
        }
        return true;
    }
}

Upvotes: 1

xio4
xio4

Reputation: 216

If I understand right you can use something like this:

import java.util.Set;
import java.util.HashSet;
public class Equaller
{
    public static boolean checkEqual(Integer[][] array) {
        Set<Integer> firstSet = new HashSet<Integer>();
        Set<Integer> secondSet = new HashSet<Integer>();

        if (array.length > 0) {
            // Fill first set
            final Integer[] firstArray = array[0];
            for (int i=0; i < firstArray.length; ++i) {
                firstSet.add(firstArray[i]);
            }
            // Compare 2D array with first set
            for (int i=1; i < array.length; ++i) {
                final Integer[] intArray = array[i];
                for (int j=0; j < intArray.length; ++j) {
                    if (!firstSet.contains(intArray[j])) {
                        return false;
                    }
                }
            }
            //  Create second set
            if (array.length > 1) {
                final Integer[] secondArray = array[1];
                for (int i=0; i < secondArray.length; ++i) {
                    secondSet.add(secondArray[i]);
                }

            // Compare first array
            if (firstSet.size() != secondSet.size()) {
                return false;
            }
            }
            return true;
        }
        return false;
    }

    public static void main(String[] argv) {
        Integer[][] aa = new Integer[][] {
            {1,2,3},
            {1,2,3,3},
            {2,3,1}};
        System.out.println(checkEqual(aa));
        Integer[][] ab = new Integer[][] {
            {1,2,3},
            {2,3,4},
            {6,7,1,2}};
        System.out.println(checkEqual(ab));
    }
}

It will return

true
false

Upvotes: 1

Related Questions