2redgoose
2redgoose

Reputation: 71

Check whether two arrays have same elements in some order

A user needs to type in two arrays (array a and array b) and then the arrays need to run through a method to see if they contain the same elements in some order, ignoring duplicates.

I'm having some trouble though, I keep getting a "Cannot find symbol - method contains (int[], int)" error in my sameSet() method.

Can someone tell me why this is happening and point me in the right direction?

 /**
 * check whether two arrays have the same elements in some order, ignoring
 * duplicates. For example, the two arrays 1 4 9 16 9 7 4 9 11 and 11 11 7 9
 * 16 4 1 would be considered identical.
 */

 public static boolean sameSet(int[] a, int sizeA, int[] b, int sizeB) {
    boolean sameSets = true; 

    for (int i = 0; i < a.length; i++) {
        if (!contains(b, a[i])) { // This line right here is where I'm having trouble. I get "Cannot symbol - method contains (int[], int)" 
            sameSets = false;
        }
    }
    for (int i = 0; i < b.length; i++) {
        if (!contains(a, b[i])) { // This line also has the same problem as the above.
            sameSets = false;
        }
    }
    return sameSets;
}


// main method used to collect user input, then pass input to sameSet method

    public static void main() { 

    final int LENGTH = 10;
    int sizeA = 0;
    int sizeB = 0;
    int a[] = new int[LENGTH];
    int b[] = new int[LENGTH];

    Scanner input = new Scanner(System.in);
    Scanner in = new Scanner(System.in);

    System.out.println("Please fill up values for array a, Q to quit: ");
    while (input.hasNextInt() && sizeA < a.length) {
        a[sizeA] = input.nextInt();
        sizeA++;

    }

    System.out.println("Please fill up values for array b, type in Q to quit: ");
    while (in.hasNextInt() && sizeB < b.length) {
        b[sizeB] = in.nextInt();
        sizeB++;
    }
    for (int i : b) {
        System.out.print(i + " | "); // For testing

    }

   sameSet(a, sizeA, b, sizeB);

}

Upvotes: 1

Views: 1370

Answers (3)

Rusheel Jain
Rusheel Jain

Reputation: 843

contains??? as per me you either need to declare this method or need to use Sets where 'contains' is provided as a valid operation

better is to use sets for your requirement

Upvotes: 0

R2-D2
R2-D2

Reputation: 1624

You can create a HashSet from each array, then use set1.equals(set2).

Upvotes: 1

Connorelsea
Connorelsea

Reputation: 2468

It's saying that the method "contains" doesn't exist. There is no "contains method" in your code.

Upvotes: 0

Related Questions