0

let's say I have two array;

Integer[] array= { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 200, 5, 6, 5 };
Integer[] array2= { 12, 2 ,3, 2, 200, 5 };

Im trying to make a method that return an array with all the element removed except those who are in also present in array2, so the output of this method should be

{2 ,3, 5, 200, 5, 5 }

I dont want to use another data structure and i have no idea how to code what im trying to do, im not sure how i can determinate the resulting array length

thanks

Kick Buttowski
  • 6,709
  • 13
  • 37
  • 58
Bob J
  • 325
  • 4
  • 13
  • 1
    How would you do it _conceptually_? – Sotirios Delimanolis Oct 06 '14 at 01:28
  • 3
    _" i have no idea how to code what im trying to do"_ - spend some time and think about it outside of code, then come up with code to match what you thought up. As is, it sounds a bit like a homework problem... – Krease Oct 06 '14 at 01:35
  • possible duplicate of [Common elements in two lists](http://stackoverflow.com/questions/5943330/common-elements-in-two-lists) – Kick Buttowski Oct 06 '14 at 01:40
  • possible duplicate of [Comparing and removing elements not present in a List and array java](http://stackoverflow.com/questions/19270115/comparing-and-removing-elements-not-present-in-a-list-and-array-java) – Ken de Guzman Oct 06 '14 at 01:41

2 Answers2

2

Here is a solution which uses only arrays and no other data structure at all. The retainAll method will return an array with some nulls in it. You can make some code to use that array and create an array with no nulls. Its really easy.

import java.util.Arrays;

public class Test {

    public static void main(String[] args) {
        Integer[] array = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 200, 5, 6, 5 };
        Integer[] array2 = { 12, 2, 3, 2, 200, 5 };

        Integer[] res = retainAll(array, array2);
        String str = Arrays.toString(res);
        System.out.println(str);
        res = removeArrayDuplicates(res);
        str = Arrays.toString(res);
        System.out.println(str);

    }

    public static Integer[] retainAll(Integer[] a, Integer[] b) {

        int ln1 = a.length;
        int ln2 = b.length;

        Integer[] res = new Integer[(ln1 < ln2) ? ln1 : ln2];
        Integer[] small = (ln1 < ln2) ? a : b;
        Integer[] big = (ln1 < ln2) ? b : a;

        boolean found = false;

        for (int i = 0; i < small.length; i++) {
            found = arrayContains(big, small[i]);
            if (found == true) {
                res[i] = small[i];
            }

        }

        return res;

    }

    public static Integer[] removeArrayDuplicates(Integer[] a) {

        int len = a.length;
        int dups = 0;
        boolean noNulls = false;

        for (int i = 0; i < len; i++) {

            for (int j = i + 1; j < len; j++) {

                noNulls = a[i] != null && a[j] != null;

                if (noNulls && a[i].equals(a[j])) {
                    a[j] = null;
                    dups++;
                }
            }

        }

        return a;

    }

    public static boolean arrayContains(Object[] a, Integer b) {
        boolean contains = false;

        for (Object c : a) {
            if (c != null && c.equals(b)) {
                contains = true;
                break;
            }
        }

        return contains;
    }

}
Erran Morad
  • 4,563
  • 10
  • 43
  • 72
1

If I understand your question, you could begin by creating a contains(Integer[], Integer) method. Iterate the array and return true if the array contains the value.

private static boolean contains(Integer[] a, Integer v) {
    for (Integer t : a) {
        if (t.equals(v)) {
            return true;
        }
    }
    return false;
}

Then you can leverage that to iterate your arrays twice. Once to perform a count, and the second time to populate a newly created array with the count number of elements. Something like,

public static Integer[] retainAll(Integer[] a, Integer[] b) {
    int count = 0;
    for (Integer val : a) {
        if (contains(b, val)) {
            count++;
        }
    }
    Integer[] out = new Integer[count];
    count = 0;
    for (Integer val : a) {
        if (contains(b, val)) {
            out[count++] = val;
        }
    }
    return out;
}

Then to test it,

public static void main(String[] args) {
    Integer[] array = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 200, 5, 6, 5 };
    Integer[] array2 = { 12, 2, 3, 2, 200, 5 };
    System.out.println(Arrays.toString(retainAll(array, array2)));
}

Output is the requested

[2, 3, 5, 200, 5, 5]

Of course, you could also use Arrays.asList(T...) and retainAll() like

public static Integer[] retainAll(Integer[] a, Integer[] b) {
    List<Integer> al = new ArrayList<>(Arrays.asList(a));
    al.retainAll(Arrays.asList(b));
    return al.toArray(new Integer[al.size()]);
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249