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

前端 未结 14 1559
予麋鹿
予麋鹿 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:22

    I'm not sure why your current code is failing (what is the Exception you get?), but I would like to point out this approach performs O(N-squared). Consider pre-sorting your input arrays (if they are not defined to be pre-sorted) and merging the sorted arrays:

    http://www.algolist.net/Algorithms/Merge/Sorted_arrays

    Sorting is generally O(N logN) and the merge is O(m+n).

提交回复
热议问题