There are 2 int [] arrays containing Drawable elements.

int[] m1 = new int[]{R.drawable.img1, R.drawable.img2, R.drawable.img3} int[] m2 = new int[]{R.drawable.img1, } 

It is necessary to find the difference, compare those 2 arrays and leave only non-repeating elements.

The order is not important. Duplication of values ​​in arrays is excluded. It is initially known that M1 contains 50 elements of M2, they only need to be removed from there.

For a long time there was no answer, I decided like this: Thanks for the help.

 private int[] delArray(int[] a, int[] b) { List<Integer> list_A = new ArrayList<Integer>(); for (int index = 0; index < a.length; index++) { list_A.add(a[index]); } List<Integer> list_B = new ArrayList<Integer>(); for (int index = 0; index < b.length; index++) { list_B.add(b[index]); } list_A.removeAll(list_B); int[] ret = new int[list_A.size()]; for(int i = 0;i < ret.length;i++) ret[i] = list_A.get(i); return ret; } 
  • 2
    Need more details. Is order important, is it possible to duplicate values ​​in the original arrays. Set.removeAll will help you in the simplest case, but it does not preserve the order and does not allow duplication of elements - awesoon
  • @soon updated the description. - St. Ivan

2 answers 2

Try this:

 final int[] a1 = {12, 14, 15, 62, 12, 23}; final int[] a2 = {1, 12, 32, 23, 22}; List<Integer> intList = new ArrayList<Integer>() {{ for (int a : a1) add(a); }}; List<Integer> intList_2 = new ArrayList<Integer>() {{ for (int a : a2) add(a); }}; intList.removeAll(intList_2); for (int a : intList) System.out.println(a); 

Outcome: 14, 15, 62

  • As far as I understand, your version eventually displays a List <Integer>, there is not enough inverse conversion. - St. Ivan

If the order of the elements is unimportant and the input arrays do not contain duplicate elements, then the simplest way is to create a Set from an array and remove from it all the elements contained in the second array:

The only problem is how to normally create a Set from an array. The standard HashSet constructor takes Collection<T> , so you need to convert int[] to a list (for example). If there is support for Google Guava, then you can use the standard method Ints.asLists . Its complexity in the documentation is not specified, but if you look at the implementation, you can see that this is O (1):

 int[] m1 = new int[]{1, 2, 3, 4, 5}; int[] m2 = new int[]{0, 4, 2, 8}; Set<Integer> s = new HashSet<>(Ints.asList(m1)); s.removeAll(Ints.asList(m2)); s.forEach(System.out::println); 

Otherwise, you can create your own method for creating a set from an integer array:

 public static Set<Integer> asSet(int[] arr) { Set<Integer> s = new HashSet<>(); for (int x : arr) { s.add(x); } return s; } 

Well, if there is support for Java 8 (I think it’s inappropriate in the framework of Android, but still), you can use Stream :

 Set<Integer> s = IntStream.of(m1).boxed().collect(toSet()); s.removeAll(IntStream.of(m1).boxed().collect(toList())); s.forEach(System.out::println); 

If there are few elements, then you can do without sets, but simply check all the elements (caution: quadratic complexity):

 IntStream.of(m1).filter(x -> IntStream.of(m2).noneMatch(y -> x == y)).forEach(System.out::println); 
  • Very valuable recommendations, thank you. - St. Ivan
  • And do not tell me what kind of magic you can convert an array to set for O (1). Interested in the idea of ​​the algorithm in relation to the standard stukrutarm, you can link. - pavel
  • one
    @pavel, pay attention, there is not an array in a set, but an array in the list for O (1). For O (1) array in the set, of course, does not work. - awesoon
  • @pavel The salt is that the HashSet constructor does not allow you to directly pass int[] , so you have to dodge with the creation of the sheet. Updated the answer. - awesoon
  • Thanks, I always thought creating a list from an array would be O (n). This speed is achieved by the fact that inside the list will lie inside the array? Those. editing the array, we change the list? - pavel