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

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

    Your second for loop should have j++ instead of i++

    0 讨论(0)
  • 2020-11-28 12:18

    Firstly remove duplicates:

    arrayList1.removeAll(arrayList2);
    

    Then merge two arrayList:

    arrayList1.addAll(arrayList2);
    

    Lastly, sort your arrayList if you wish:

    collections.sort(arrayList1);
    

    In case you don't want to make any changes on the existing list, first create their backup lists:

    arrayList1Backup = new ArrayList(arrayList1);
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-28 12:20

    Add elements in first arraylist

    ArrayList<String> firstArrayList = new ArrayList<String>();
    
    firstArrayList.add("A");
    firstArrayList.add("B");
    firstArrayList.add("C");
    firstArrayList.add("D");
    firstArrayList.add("E");
    

    Add elements in second arraylist

    ArrayList<String> secondArrayList = new ArrayList<String>();
    
    secondArrayList.add("B");
    secondArrayList.add("D");
    secondArrayList.add("F");
    secondArrayList.add("G");
    

    Add first arraylist's elements in second arraylist

    secondArrayList.addAll(firstArrayList);
    

    Assign new combine arraylist and add all elements from both arraylists

    ArrayList<String> comboArrayList = new ArrayList<String>(firstArrayList);
    comboArrayList.addAll(secondArrayList);
    

    Assign new Set for remove duplicate entries from arraylist

    Set<String> setList = new LinkedHashSet<String>(comboArrayList);
    comboArrayList.clear();
    comboArrayList.addAll(setList);
    

    Sorting arraylist

    Collections.sort(comboArrayList);
    

    Output

     A
     B
     C
     D
     E
     F
     G
    
    0 讨论(0)
  • 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).

    0 讨论(0)
  • 2020-11-28 12:23
    **Add elements in Final arraylist,**
    **This will Help you sure**
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class NonDuplicateList {
    
    public static void main(String[] args) {
    
        List<String> l1 = new ArrayList<String>();
        l1.add("1");l1.add("2");l1.add("3");l1.add("4");l1.add("5");l1.add("6");
        List<String> l2 = new ArrayList<String>();
        l2.add("1");l2.add("7");l2.add("8");l2.add("9");l2.add("10");l2.add("3");
        List<String> l3 = new ArrayList<String>();
        l3.addAll(l1);
        l3.addAll(l2);
        for (int i = 0; i < l3.size(); i++) {
            for (int j=i+1; j < l3.size(); j++) {
                 if(l3.get(i) == l3.get(j)) {
                     l3.remove(j);
                }
            }
        }
        System.out.println(l3);
    }
    

    }

    Output : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

    0 讨论(0)
提交回复
热议问题