如果某个元素在数组列表中重复,如何删除该元素的所有出现[重复]

问题描述 投票:0回答:4

我有一个值为 {"16","b","c","d","e","16","f","g","16","b"} 的列表; 在这个 16 和 b 中重复,所以我想删除它们的所有条目,并且我需要输出为 c、d、e、f、g。下面的程序运行良好。还有更好的解决方案吗?

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;
 }
}
java arraylist duplicates
4个回答
2
投票

一种方法是使用流来查找每个元素的频率:

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]

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);
         }

1
投票

您可以对每个元素进行比较

index
lastIndex
。如果它们是
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
投票

我想这样就可以了

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);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.