Prashanth Sams
Prashanth Sams

Reputation: 21129

Compare and match arrays with different sizes

I have a couple of arrays with different sizes; say, array A and array B.

Array A

[chery, chery, uindy, chery, chery]

Array B

[chery, uindy]

Need to check whether the values present in Array A is available in Array B or not. In the above example, all the values in Array A is available in Array B. Please help this out with the Java code. Thanks!

Upvotes: 1

Views: 5130

Answers (6)

Raedwald
Raedwald

Reputation: 48684

An array is not a good data structure for doing this. A Set is better. So convert your two arrays to Set objects, then simply use Set.equals(). Either do the conversion by creating new objects just before the comparison, or use a Set everywhere.

Upvotes: 0

Makoto
Makoto

Reputation: 106430

You can convert your arrays to a List and then use the containsAll method to see if a particular list contains all elements described in another list.

You would get better performance out of it if they were Sets instead.

Example:

List<String> firstList = Arrays.asList("chery", "chery", "unid", ...);
List<String> secondList = Arrays.asList("chery", "unid", ...);

System.out.println(secondList.containsAll(firstList));

If the performance of this method in particular is getting a bit dodgy, then consider converting the lists into Sets instead:

Set<String> firstSet = new HashSet<>(Arrays.asList("chery", "chery", "unid", ...));

Upvotes: 4

Adrian Shum
Adrian Shum

Reputation: 40036

Base on your requirement, you are going to find out if B is a superset of A (I mean the distinct values).

This can be easily done by one line like this:

String[] aArr = {.....};
String[] bArr = {.....};

return new HashSet<String>(Arrays.asList(bArr)).containsAll(Arrays.asList(aArr));

In brief, make B a Set, and check if B set contains all values of A

so, if A = {Apple, Apple, Banana, Cherry} and B = {Apple, Banana, Cherry, Pineapple}, it will return true (that's the behavior base on your description)

Upvotes: 1

Deepak
Deepak

Reputation: 1545

In the example I am using integers but can be used for other types also with slight modifications. First put a loop on array A elements.

for(int i =0; i<A.length(); i++)
{
    //this loop will transverse with all elements in array A.
}

Now inside this for loop make another for loop which transverse through elements of loop B.

for(int i =0; i<A.length(); i++)
{
    for(int j=0; j<B.length();j++)
        {
            if(A[i] == B[j])
                { System.out.println("this element is in array A and B"); }
        }

}

Now if you want to check if all elements of A are in B you can make a boolean. this boolean is true as long each element in A is found at least once in B. as soon as you find one element which is not present on both arrays you can exit.

Upvotes: 1

PDStat
PDStat

Reputation: 5825

Set<String> setA = new HashSet<>(Arrays.asList(new String[]{"chery", "chery", "uindy", "chery", "chery"}));
Set<String> setB = new HashSet<>(Arrays.asList(new String[]{"chery", "uindy"}));

System.out.println("Sets are equal: " +setA.equals(setB));

The equals method of AbstractSet says

Compares the specified object with this set for equality. Returns true if the given object is also a set, the two sets have the same size, and every member of the given set is contained in this set. This ensures that the equals method works properly across different implementations of the Set interface. This implementation first checks if the specified object is this set; if so it returns true. Then, it checks if the specified object is a set whose size is identical to the size of this set; if not, it returns false. If so, it returns containsAll((Collection) o).

Upvotes: -1

Christophe Schutz
Christophe Schutz

Reputation: 613

For arrays of Strings :

for (String str : array1)
{
    System.out.println(ArrayUtils.contains(array2, str);
}

Upvotes: 0

Related Questions