2017-06-15 16 views
2

我有一個值爲{「16」,「b」,「c」,「d」,「e」,「16」,「 F」, 「G」, 「16」, 「b」}; 在這16和b重複,所以我想刪除它們的所有條目,我需要輸出爲c,d,e,f,g。以下程序正常工作。有更好的解決方案如何刪除一個元素的所有發生如果它在一個arraylist中重複

public class Test { 

public static void main(String[] args) { 

    ArrayList <String> l = new ArrayList <String>(); 
    String[] str = { 
    "16", 
    "b", 
    "c", 
    "d", 
    "e", 
    "16", 
    "f", 
    "g", 
    "16", 
    "b" 
    }; 


    for (String s: str) { 
    l.add(s); 
    } 
    List ll = removeDups(l); 
    l.removeAll(ll); 
    System.out.println("Final List " + l); 
} 

private static List <String> removeDups(ArrayList <String> l) { 
    List <String> ll = new ArrayList <String>(); 
    for (String a: l) { 
    int x = Collections.frequency(l, a); 
    if (x > 1) { 
    ll.add(a); 
    } 
    } 
    return ll; 
} 
} 
+0

請在發佈Stackoverflow問題之前解釋代碼或發表評論。它幫助每個人。 –

+0

實際上,'removeDups'方法實際上應該稱爲'findDups',因爲它實際上並沒有刪除任何東西;它只是_finds_重複的項目。如果'l.removeAll(ll)'這一行被移到'removeDups'內,那麼_then_'removeDups'實際上會有這個名字。 –

+0

考慮Collections.frequency的命令是'O(n)',你可以使用Hash地圖做得更好。 –

回答

1

您可以比較indexlastIndex。如果它們是same,則該元素是unique。我們可以過濾這些元素。

// imports 
import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.List; 

// sample code 
String[] str = {"16","b","c","d","e","16","f","g","16","b"}; 
List<String> list = Arrays.asList(str); // List from the array 
List<String> newList = new ArrayList<String>(); 
for(String myStr : list){ 
    if(list.indexOf(myStr) == list.lastIndexOf(myStr)){ 
     /* 
     * This is a unique element as its index and lastIndex in list are same. 
     * Add it to new list. 
     */ 
     newList.add(myStr); 
    } 
} 
// Freeing resources 
str = null; 
list = null; 

System.out.println("Final List: "+ newList); 
+0

哇。它的工作正常。非常感謝。 –

+0

@Sammetanagasrinivas你可以請upvote答案並接受答案作爲解決方案? –

2

您可以使用Set從給定的數組列表中刪除重複的元素。

下面是示例代碼:

Set<String> myStrSet = new HashSet<String>(); 
Set<String> duplicateSet = new HashSet<String>(); 

     for(String str : myArrayList){ 
      if(myStrSet.contains(str)){ 
        duplicateSet.add(str); 
      } else { 
        myStrSet.add(str); 
      } 
     } 

     for(String str : duplicateSet){ 
      myStrSet.remove(str); 
     } 

     for(String str : myStrSet){ 
      System.out.println("Print non-duplicate elements : " + str); 
     } 
+2

我想OP想刪除重複數組元素的_all_實例;這會留下每個重複項目的實例。 –

+0

是的,我想刪除所有的出現,如果重複發生,不想有任何發生。 –

+0

我不知道這個答案如何佔用投票,它有多個錯字(myStrList和st),它不會刪除重複項。 –

1

一種方法是使用流來發現每個元素的頻率:

Map<String, Long> counts = yourList.stream() 
    .collect(Collectors.groupingBy(
     Function.identity(),  // keep the element as the key 
     Collectors.counting())); // values will be the count 

然後,你可以使用removeIf除去基於元素一個條件,您將使用上面計算出的頻率圖:

yourList.removeIf(elem -> counts.get(elem) > 1); 

System.out.println(yourList); // [c, d, e, f, g] 

另一種方法是首先找出哪些值有重複,哪些是唯一的。對於這一點,我們可以使用一個Map<String, Boolean>

Map<String, Boolean> duplicates = new LinkedHashMap<>(); 
yourList.forEach(elem -> duplicates.compute(elem, (k, v) -> v != null)); 

在這裏我遍歷列表,並且對於每一個元素,我把它變成地圖,計算值true如果元素已經存在一個密鑰或false,如果它是唯一的。

然後,您可以在列表上使用removeIf,與簡單地從地圖返回值的謂詞:

yourList.removeIf(duplicates::get); 

System.out.println(yourList); // [c, d, e, f, g] 
+0

我認爲你的意思是低於禮儀。 (String s:l){duplicateates.put(s,(duplicates.containsKey(s)?true:false));} Iterator it = l.listIterator(); \t \t while(it.hasNext()){ \t \t \t String ss =(String)it.next(); \t \t if(duplicates.get(ss)){ \t \t \t it.remove(); \t \t \t} \t \t} –

+0

@Sammetanagasrinivas是的,這似乎也工作。你不需要三元運算符。只需'duplicateates.put(s,duplicate.containsKey(s));'很好。 –

0

我認爲這會做

public class DeleteDuplicates { 

    public static void main(String[] args) { 

     String[] str={"16","b","c","d","e","16","f","g","16","b"}; 
     List<String> l= new ArrayList<String>(); 
     Set<String> set = new HashSet<String>(); 

     for(String string : str) { 

      if(set.add(string)) 
       l.add(string); 
      else 
       l.remove(string); 
     }    

     System.out.println(l); 
    } 
} 
相關問題