Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static int[] intersectionOfTwoArrays(int[] firstArr, int[] secondArr) {
- if (firstArr.length > secondArr.length) {
- return intersectArrays(firstArr, secondArr);
- } else {
- return intersectArrays(secondArr, firstArr);
- }
- }
- private static int[] intersectArrays(int[] firstArr, int[] secondArr) {
- int[] result = new int[secondArr.length];
- for (int i = 0; i < firstArr.length; i++) {
- for (int j = 0; j < secondArr.length; j++) {
- if (firstArr[i] == secondArr[j]) {
- result[j] = secondArr[j];
- }
- }
- }
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement