compare two arrays for common Items and return another array with the common items

So the problem is I have two array and have to check them for common items.Usual stuff, very easy.But the tricky thing for me is that I have to return another array with the elements that have been found to be common.I cannot not use any Collections.Thanks in advance.This is my code so far!

public class checkArrayItems {
    static int[] array1 = { 4, 5, 6, 7, 8 };
    static int[] array2 = { 1, 2, 3, 4, 5 };

    public static void main(String[] args) {
        checkArrayItems obj = new checkArrayItems();
        System.out.println(obj.checkArr(array1, array2));

    }

    int[] checkArr(int[] arr1, int[] arr2) {
        int[] arr = new int[array1.length];
        for (int i = 0; i < arr1.length; i++) {
            for (int j = 0; j < arr2.length; j++) {
                if (arr1[i] == arr2[j]) {
                    arr[i] = arr1[i];
                }

            }
        }
        return arr;

    }

}

Upvotes: 2

Views: 1642

Answers (4)

Shubhendu Pramanik
Shubhendu Pramanik

Reputation: 2751

You can use a MIN or MAX default dummy value for the elements in your new array arr using arr[i] = Integer.MIN_VALUE;. In that way you will be able to differentiate between the real and dummy values. Like below:

int[] checkArr(int[] arr1, int[] arr2) {
        int[] arr = new int[array1.length];
        for (int i = 0; i < arr1.length; i++) {
            arr[i] = Integer.MIN_VALUE;
            for (int j = 0; j < arr2.length; j++) {
                if (arr1[i] == arr2[j]) {
                    arr[i] = arr1[i];
                }

            }
        }
        return arr;

    }

Output

[4, 5, -2147483648, -2147483648, -2147483648]

EDIT

Conclusion When you iterate over arr all the values other than -2147483648 are common.

EDIT 2

To print the common values as mentioned on the comment below:

public static void main(String[] args) {
checkArrayItems obj = new checkArrayItems();
int[] arr = obj.checkArr(array1, array2);
        System.out.println("Common values are : ");
        for (int x : arr) {
            if (x != Integer.MIN_VALUE) {
                System.out.print(x+"\t");
            }
        }
}

Suggestion: Follow naming convention for class i.e. make checkArrayItems to CheckArrayItems.

Upvotes: 0

Piotr Wilkin
Piotr Wilkin

Reputation: 3491

In case someone was wondering how the "chasing" algorithm mentioned by @user3438137 looks like:

int[] sorted1 = Arrays.copyOf(array1, array1.length);
Arrays.sort(sorted1);
int[] sorted2 = Arrays.copyOf(array2, array2.length);
Arrays.sort(sorted2);
int[] common = new int[Math.min(sorted1.length, sorted2.length)];
int numCommonElements = 0, firstIndex = 0; secondIndex = 0;
while (firstIndex < sorted1.length && secondIndex < sorted2.length) {
    if (sorted1[firstIndex] < sorted2[secondIndex]) firstIndex++;
    else if (sorted1[firstIndex] == sorted2[secondIndex]) {
        common[numCommonElements] = sorted1[firstIndex];
        numCommonElements++;
        firstIndex++;
        secondIndex++;
    }
    else secondIndex++;
}
// optionally trim the commonElements array to numCommonElements size

Upvotes: 3

Marcos Vasconcelos
Marcos Vasconcelos

Reputation: 18276

Declare an index before the two for loops

int index = 0;

that will hold the current position of the arr array. Then:

arr[index++] = arr1[i];

And also, since you initialize arr with arr1.lenght your array will be filled with 0s at the end of the not colision.

Upvotes: 0

user3438137
user3438137

Reputation: 19

I'm lazy to type the code, but here is the algorithm. 1. sort both array 2. iterate over array comparing items and increasing the indexes.

Hope this helps.

Upvotes: 0

Related Questions