1

The method which takes two integer arrays. The method returns an integer array containing all the elements in array int[]a that are also present in array int[]b in their original sequential order in int[]a.

This is what it should output :

input: {1, 2, 3, 4, 5, 6, 7, 3, 8, 9, 2, 10}, {3, 2, 7, 12, 3, 9, 5, 2}

output: {2, 3, 5, 7, 3, 9, 2}



input: {4, 7, 1, 6, 9, 2, 3, 1}, {8, 5, 2, 1, 9, 4}

output: {4, 1, 9, 2, 1}

I tried something like this just to test it out to get my head around it but I still couldn't.

    public static void arraysMatching(int[] s1, int[] s2) {
    ArrayList<Integer> storage = new ArrayList<Integer>();
        for (int i = 0; i <= s1.length; i++) {
            for (int j = 0; j <= s2.length; j++) {
                if (s2[j] == s1[i]) {
                    storage.add(s2[j]);
                    break;
                }
            }
            break;
        }
        System.out.println(storage);

    }

Test cases:

int [] m1 = {1, 2, 3, 4, 5, 6, 7, 3, 8, 9, 2, 10};
int [] m2 = {3, 2, 7, 12, 3, 9, 5, 2};
System.out.println(Arrays.toString(arraysMatching(m1, m2)));
Should print out [2, 3, 5, 7, 3, 9, 2]
5
  • Possible duplicate of Finding the intersection of two arrays Commented Mar 27, 2017 at 8:46
  • Ill edit my question Commented Mar 27, 2017 at 8:47
  • I feel its not the same Commented Mar 27, 2017 at 8:47
  • What exactly is the problem? How does the actual behaviour differ from the expected behaviour? Commented Mar 27, 2017 at 8:50
  • I get an error on this line System.out.println(Arrays.toString(arraysMatching(m1, m2))); saying that its not applicable for the type void Commented Mar 27, 2017 at 8:55

2 Answers 2

2

To add to Codors answer, have the method return int[]:

public static int[] arraysMatching(int[] s1, int[] s2) {

    ArrayList<Integer> storage = new ArrayList<>();
    //for (int i = 0; i <= s1.length; i++) {
    //wrong. change <= to  < or better:
    for (int i : s1) {

        //for (int j = 0; j <= s2.length; j++) {
        //wrong. change <= to  < or better:
        for (int j : s2) {
            if (j == i) {
                storage.add(j);
                break;
            }
        }
    }

    //if you can use streams do
    //return  storage.stream().mapToInt(i->(int)i).toArray();

    //alternatively
    int[] returnArray = new int[storage.size()];
    for(int i=0; i< storage.size(); i++) {
        returnArray[i]=storage.get(i);
    }
    return returnArray;
}

So you can use it with System.out.println(Arrays.toString(arraysMatching(m1, m2)));

Sign up to request clarification or add additional context in comments.

1 Comment

This seems to be working but I have never learnt about mapToInt is there an alternative to that ?
1

To my understanding, the second break statement terminates the outer loop too early; furthermore, the indexing goes one step too far in both loop termination conditions. Change the implementation as follows.

public static void arraysMatching(int[] s1, int[] s2) {
    ArrayList<Integer> storage = new ArrayList<Integer>();
    for (int i = 0; i < s1.length; i++) {
        for (int j = 0; j < s2.length; j++) {
            if (s2[j] == s1[i]) {
                storage.add(s2[j]);
                break;
            }
        }
    }
    System.out.println(storage);
}

Edit:

If the result is not to be printed but to be returned, the signature and the body have to be changed as follows.

public static ArrayList<Integer> arraysMatching(int[] s1, int[] s2) {
    ArrayList<Integer> storage = new ArrayList<Integer>();
    for (int i = 0; i < s1.length; i++) {
        for (int j = 0; j < s2.length; j++) {
            if (s2[j] == s1[i]) {
                storage.add(s2[j]);
                break;
            }
        }
    }
    return storage;
}

Edit:

If the return type is required to be Integer[], the implementation has to be changed as follows.

public static Integer[] arraysMatching(int[] s1, int[] s2) {
    ArrayList<Integer> storage = new ArrayList<Integer>();
    for (int i = 0; i < s1.length; i++) {
        for (int j = 0; j < s2.length; j++) {
            if (s2[j] == s1[i]) {
                storage.add(s2[j]);
                break;
            }
        }
    }
    int[] result = new int[storage.size()];
    result = storage.toArray(result);
    return result;
}

Edit:

If the return type is required to be int[] and no 'fancy stuff' for result conversion is desired, the code has to be changed as follows.

public static int[] arraysMatching(int[] s1, int[] s2) {
    ArrayList<int> storage = new ArrayList<int>();
    for (int i = 0; i < s1.length; i++) {
        for (int j = 0; j < s2.length; j++) {
            if (s2[j] == s1[i]) {
                storage.add(s2[j]);
                break;
            }
        }
    }
    int[] result = new int[storage.size()];
    for (int k = 0; k < storage.length(); k++) {
        result[k] = storage.get(k);
    }
    return result;
}

10 Comments

Hold on sorry I forgot to add the testcases
Please take a look at my edited post It somehow gives me an error when i print this line out System.out.println(Arrays.toString(arraysMatching(m1, m2)));
arraysMatching does not return anything so you can't print it
Gives an error with my test case. The method toString(long[]) in the type Arrays is not applicable for the arguments (void)
This is most probably because 1, which is the first element in the first argument, does not occur in the second argument; due to the second break statement, the result does not get added anything.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.