Page 1 of 1

One of the variations on a merge operation is to eliminate any duplicates whlie performing the merge. So the resulting m

Posted: Sun May 15, 2022 1:34 pm
by answerhappygod
One Of The Variations On A Merge Operation Is To Eliminate Any Duplicates Whlie Performing The Merge So The Resulting M 1
One Of The Variations On A Merge Operation Is To Eliminate Any Duplicates Whlie Performing The Merge So The Resulting M 1 (31.17 KiB) Viewed 67 times
One of the variations on a merge operation is to eliminate any duplicates whlie performing the merge. So the resulting merged array may be shorter than the sum of the lengths of the origina arrays. We will assume though, that the two input arrayLists of Objects each are unique, but sorted of course. The merge below uses ArrayLists (like Python lists) so we use the get() and add methods for array access. public static ArrayList mergeUnique (ArrayList a, ArrayList b) { int i = 0; int j = 0; int cmp; ArrayList m = new ArrayList; while (i < a.size() && . O if ((c = .get(i).compareTo( .get >))==0){ i++; // skip a duplicate } else if ( < 0){ m.add .get(i++)); } else { m.add( .get(j++)); } } 1/ copy remaining elements over while (i < 0) m.add(a. ++)); while (< . 0) m.add(a. ++)); return ; }