1

Is it possible to subtract 2 String arrays to produce a new array? In the following code, I am calling to Methods I wrote for other Array operations. I want to create a new array using the results of those Methods. I need to create the new array in one line of code.

Since this is a HW assignment, I am not looking for the code to be written for me. Just a little direction.

static String[] xor( String[] set1, String[] set2 )
{
    set1 = (union(set1, set2) - intersection(set1, set2)); 
    return set1; 
}
Abhijith Nagarajan
  • 3,865
  • 18
  • 23
user2884866
  • 39
  • 1
  • 3
  • 2
    What language are you using? – Arunas Oct 16 '13 at 15:27
  • You would first have to define what subtraction means, as arrays are ordered collections, not sets. For example, will the result of `[a, a] - [a]` be `[a]` (the remaining `a` element) or `[]` (an empty set)? Will the result of `[a, b] - [b, a]` be `[]` (because both elements were removed) or `[a, b]` (because the subsequence `[b, a]`, in that order, was not found and thus cannot be removed)? – O. R. Mapper Oct 16 '13 at 15:34
  • Sorry, should have been more clear. Java. – user2884866 Oct 16 '13 at 15:34
  • @user2884866: Please add that bit of information (the language) as a tag. – O. R. Mapper Oct 16 '13 at 15:35
  • O.R. Mapper: I added the tag. I am new to this site (and programming as well). Much to learn. – user2884866 Oct 16 '13 at 15:38
  • Why was the tag `string` added? This is not specific to strings in any conceivable way ... – O. R. Mapper Oct 16 '13 at 15:48
  • This [answer](http://stackoverflow.com/questions/5283047/intersection-union-of-arraylists-in-java) on stackoverflow will solve your problem. Only use it only if all your ideas fail :) What @sean-patrick-floyd suggested is helpful. – MDrabic Oct 16 '13 at 15:50

4 Answers4

0

I would store the contents of both arrays in a Set (one each), as Sets are optimized for this kind of operation (look at the add/addAll, remove/removeAll, retainAll methods).

Read the Java Tutorial bit about Sets for further reference.

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • 3
    As a new programmer, and a homework assignment, I suspect using a `Set` may be outside of the scope of OP's knowledge / task. – dimo414 Oct 16 '13 at 15:45
  • I disagree (at least about the "as a new programmer" part). any solution with direct array manipulation will be much uglier and trickier – Sean Patrick Floyd Oct 16 '13 at 15:47
  • Well yes, but the collections framework and the underlying data structures may not have been covered in OP's class yet. The fact that the task uses arrays to hold conceptual sets suggests this. – dimo414 Oct 16 '13 at 15:50
  • Thank you for your help. It turns out I was ignoring another Method I had already written in the program. I am actually a little embarrassed that I missed it. The following code produced the correct answer for my HW assignment. – user2884866 Oct 16 '13 at 15:51
  • 1
    static String[] xor( String[] set1, String[] set2 ) { set1 = difference(union(set1, set2), intersection(set1, set2)); return set1; } – user2884866 Oct 16 '13 at 15:51
  • Rather than "not having been covered in OP's class" (which is always a ridiculous excuse for not using a given type or method, as documentation is of course available), the issue may rather be that the spirit of the assignment is probably learning to think about and work with arrays, not using ready-made code (be it in the standard library or not). – O. R. Mapper Oct 16 '13 at 15:53
  • That's more what I meant, however I disagree that "not being covered" is a ridiculous excuse. Particularly in introductory programming courses, handing a new programmer a pile of documentation on a collections framework, when they've likely not yet even seen arraylists or generics, let alone more advanced data structures like hashmaps, is only going to confuse. Let the teacher introduce these topics as appropriate. – dimo414 Oct 16 '13 at 16:06
  • If I may add a comment, as a newcomer to this field, I am overwhelmed by the different options available to solve a problem. Furthermore, I feel as though the concepts taught in class are not always the cleanest and most efficient way to solve the problem. I can only assume we will learn to program more efficiently as the class progresses. – user2884866 Oct 16 '13 at 16:24
0

There are a few ways you may go.

If you know about collections, you may wish to look at Arrays.asList, List#removeAll and List#toArray.

If you don't, however, you might use a loop.

  • Create a boolean[] as big as the union, and an int counter, starting at 0;
  • Iterate over the union;
  • For each String there, check if it is present in the intersection;
  • If it is, store false in the boolean[], at the current index;
  • If it isn't, store true instead, and add one to the counter.
  • Create the String[] you wish to return; its length is the value of the counter;
  • Iterate the boolean[] and, at the indexes where you find true pick the string with the same index from union and store it in the third array.
afsantos
  • 5,178
  • 4
  • 30
  • 54
0

Let's simplify your problem real quick. From what you're describing, you don't actually care about the arrays being sets, nor that the end result is the xor of two sets. You simply want to remove the contents of one array from another array, e.g.:

primary = {1, 2, 3, 4, 5, 6}
remove = {3, 5}
result = subtract(primary, remove) = {1, 2, 4, 6}

From your comment that "I need to create the new array in one line of code" it's likely you already have such a tool at your disposal, perhaps another method you previously implemented? If not, you likely cannot do this in one line, but it's not too hard. Simply loop over the first array (primary), and for each item in primary see if the value is found in remove, if not, add the item in primary to result. Ensuring the result array is the right size will be a little tricky, but hopefully this points you in the right direction :)

dimo414
  • 47,227
  • 18
  • 148
  • 244
  • You are correct, there was another method that I created called difference. By using it, I was able to get the desired answer. As a new programmer, I sometimes overlook those things right in front of me. Work in progress :) – user2884866 Oct 16 '13 at 16:15
0
static String[] xor( String[] set1, String[] set2 )
{
    set1 = (union(set1, set2) - intersection(set1, set2)); 

    //first create union(set1, set2)
    //create a new array called union_array that contains all values of set1
    //then add values from set2 that are unique to union_array
    //adding to arrays isn't easy.  i would recommend using an ArrayList then converting it back to an array (if your prof allows that)

    //second create intersection(set1, set2)
    //create a new array called intersection_array
    //add every value in set1 that is also in set2.
    //same as before, try to use an ArrayList and convert back to array

    //third, you want to do the "subtraction"
    //remove all cases of intersection_array inside of union_array
    //put all these values in new array:  return_array

    //return return_array;
    return set1; 
}

If you want some code on getting unions and intersection of arrays, check out:

http://www.dreamincode.net/forums/topic/170409-finding-the-union-and-intersection-of-two-arrays/