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

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

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

Post 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 65 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 ; }
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply