Help with the implementation of the minus() method, which takes parameters 2 of the arr1 and arr2 arrays. As a result, should return the difference between the two arrays.

The explanation is that in my understanding the difference is to return an array, the elements of which are contained in arr1, but not in arr2.

Closed due to the fact that the essence of the question is incomprehensible by the participants Alexey Shimansky , VAndrJ , Nick Volynkin 8 Jun '16 at 0:51 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • 2
    What do you understand the difference arrays? An operation similar to set difference? What is the catch in this case? Create a third array, an outer loop over one array, inner one over the second, check if the element of the first array is equal to the second, then these two elements are not included in the difference. If an equal element is not found in one pass through the second cycle, then add the current element of the first array to the difference array ... Give an example of input and output data. - iksuy
  • So yes - the concept of "difference of arrays" is not disclosed. Zasim answer you impossible in principle. - Yuriy SPb
  • one
    Moreover, did you try to do something or is it a test for the inhabitants of ruSO? - Alexey Shimansky
  • As it seemed to me, this is a simple operation of subtracting from the elements of one array of values ​​of the elements of another array. - DimXenon
  • one
    @Alexander Dermengee if you have a simple task, then you can use Collections instead of arrays and in particular use the removeAll method. But if you really have arrays and are firmly tied to them and the logic already has everything for them - there will be a completely different solution ..... but it would be interesting what you have and what you tried to do - Alexey Shimansky

2 answers 2

  1. We check that arrays of the same type (this is how the input data of the method is done automatically).
  2. Check that they are both not null.
  3. Check their length. The maximum length is stored in the buffer integer variable (int arrayLength). 3.1. Create a buffer array (if we do not use containers) for the result - the length of the array is equal to arrayLength.
  4. We start the cycle from 0 to arrayLength-1;
  5. If the value of the loop counter is <the length of the smaller array (you can also save it), then we subtract the value of the second array from the value of the first array. Save the result to the buffer array under the same index.
  6. If the value of the loop counter is greater than the length of the smaller array, simply subtract the value from zero. Otherwise, write the value from the first array.
  7. We return a buffer array (of course, a link to it) as a result.

    Thank you all for the thought. I think all the same decision will be through the collection. Here's what I got.

     public static Integer[] minus(Integer[] arr1, Integer[] arr2){ List<Integer> list1 = new ArrayList<Integer>(Arrays.asList(arr1)); List<Integer> list2 = new ArrayList<Integer>(Arrays.asList(arr2)); list1.removeAll(list2); Integer[] array = list1.toArray(new Integer[list1.size()]); return array; } 
    • one
      You can shorten the code a bit by returning list1.toArray(new Integer[list1.size()]); immediately list1.toArray(new Integer[list1.size()]); . - DimXenon
    • one
      list2 not needed - you can immediately Arrays.asList(...) to the removeAll method. - Pavel Mayorov