Merging two arrayLists into a new arrayList, with no duplicates and in order, in Java

前端 未结 14 1509
予麋鹿
予麋鹿 2020-11-28 11:50

I am trying to \"combine\" two arrayLists, producing a new arrayList that contains all the numbers in the two combined arrayLists, but without any duplicate elements and the

14条回答
  •  执笔经年
    2020-11-28 12:19

    Perhaps my nested for loops being used incorrectly?

    Hint: nested loops won't work for this problem. A simple for loop won't work either.

    You need to visualize the problem.

    Write two ordered lists on a piece of paper, and using two fingers to point the elements of the respective lists, step through them as you do the merge in your head. Then translate your mental decision process into an algorithm and then code.

    The optimal solution makes a single pass through the two lists.

提交回复
热议问题