#i have 2 arrays lets call it A and B and i want an extra array that outputs the difference

1 messages · Page 1 of 1 (latest)

little zinc
#

A = [0,1,2,3,4,5,6]
B = [0,1,2,3,4]

Output should be [5,6]
i would like to solve it without collections, i have done correctly the union and intersection but cant figure out how to do difference

waxen lightBOT
#

Hey, @little zinc!
Please remember to /close this post once your question has been answered!

mystic pendant
#

are both arrays sorted ?

little zinc
#

yep

#

i did it with Arrays.sort()

mystic pendant
#

Create a third array with the length of both arrays combined (Worst case no elements are equal to one another). Then use a for loop to traverse both arrays at the same time. If one of them has a greater value than the other copy all elements into the newly created third array. If you reach the end of one array put all remaining elements of the other array in your third array

cedar snow
#

If you've done the previous ones with collections, I supposed you've used retainAll()

#

Now it's time for removeAll()

little zinc
#

thank you