2017-06-15 17 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); 
    } 
} 
相关问题