I have an array arr1={4,5,8,9,2,1}
and a second arr2 = {6,5,8,2,4,5,7,1}
and i wan to remove sequence of two value from arr1 if they occur in arr2 ;and append the result in arr2.
like "4,5"
in arr1 ; after removing it will bearr1={8,9,2,1}
so arr2 ={6,5,8,2,4,5,7,1,8,9,2,1}
Asked
Active
Viewed 59 times
-2
-
Combine the two arrays, then convert to a set to get rid of duplicates. – Doga Oruc Dec 01 '19 at 16:24
-
that will remove all duplicated, however i need to remove only them if they occur in the same sequence – Med Bkb Dec 01 '19 at 16:32
-
Refer to https://stackoverflow.com/questions/5283047/intersection-and-union-of-arraylists-in-java for your answer – Rahul Parihar Dec 01 '19 at 16:34
-
thank you Rahul but the result was sorted i need them as they are – Med Bkb Dec 01 '19 at 16:46
-
Does this answer your question? [Java Remove Duplicates from an Array?](https://stackoverflow.com/questions/10056729/java-remove-duplicates-from-an-array) – S.S. Anne Jan 07 '20 at 12:36
1 Answers
-1
This is duplicated, please check similar questions, for example:
Java Remove Duplicates from an Array?
Anyways,if you can't use Set
the not that efficient way to do it is by simply having a pointer which you use to iterate through the array. If you find the first int of the two introduced (in this case, number 4) you got to check the next one. If both are the same (4,5), you erase them (put a NULL on it for example) and continue iterating from the next position after the 5. Then, when you finished iterating simply create a new array with the length being the number of positions in both arrays different than NULL and append them on this result array.

Román
- 136
- 1
- 9