Finding the different elements between two ArrayLists in Java

后端 未结 6 1276
滥情空心
滥情空心 2021-02-06 05:43

How can I know the different element between 2 array list in java? I need the exact element not a Boolean value which can be retrieved using removeAll().

6条回答
  •  灰色年华
    2021-02-06 06:10

    LinkedHashMap table;
    for each element e of array A
        if table.get(e) != null
            table.put( e, table.get(e) + 1 )
        else
           table.put( e, 0 )
    
    //Do the same for array B
    for each element e of array B
        if table.get(e) != null
            table.put( e, table.get(e) + 1 )
        else
           table.put( e, 0 )
    

    At the end of the for loops elements in table with value=0 are the different ones.

提交回复
热议问题