Remove duplicates (both values) - duplicate values

2020-06-01 02:13发布

I have an ArrayList with the following strings;

 List<String> e = new ArrayList<String>();
 e.add("123");
 e.add("122");
 e.add("125");
 e.add("123");

I want to check the list for duplicates and remove them from the list. In this case my list will only have two values, and in this example it would be the values 122 and 125, and the two 123s will go away.

What will be the best way to this? I was thinking of using a Set, but that will only remove one of the duplicates.

11条回答
欢心
2楼-- · 2020-06-01 02:40

Solution in ArrayList

public static void main(String args[]) throws Exception {
      List<String> e = new ArrayList<String>();
      List<String> duplicate = new ArrayList<String>();
      e.add("123");
      e.add("122");
      e.add("125");
      e.add("123");

      for(String str : e){
          if(e.indexOf(str) != e.lastIndexOf(str)){
              duplicate.add(str);
          }
      }

      for(String str : duplicate){
          e.remove(str);              
      }

      for(String str : e){
          System.out.println(str);
      }
  }
查看更多
Evening l夕情丶
3楼-- · 2020-06-01 02:42

With the Guava library, using a multiset and streams:

e = HashMultiset.create(e).entrySet().stream()
    .filter(me -> me.getCount() > 1)
    .map(me -> me.getElement())
    .collect(toList());

This is pretty, and reasonably fast for large lists (O(n) with a rather large constant factor). But it does not preserve order (LinkedHashMultiset can be used if that is desired) and it creates a new list instance.

It is also easy to generalise, to instead remove all triplicates for example.

In general the multiset data structure is really useful to keep in ones toolbox.

查看更多
We Are One
4楼-- · 2020-06-01 02:43
List<String> e = new ArrayList<String>();
e.add("123");
e.add("122");
e.add("125");
e.add("123");
e.add("125");
e.add("124");
List<String> sortedList = new ArrayList<String>();
for (String current : e){
    if(!sortedList.contains(current)){
        sortedList.add(current);
    }
    else{
        sortedList.remove(current);
    }
}
e.clear();
e.addAll(sortedList);
查看更多
萌系小妹纸
5楼-- · 2020-06-01 02:48

Something like this (using a Set):

Set<Object> blackList = new Set<>()

public void add(Object object) {
    if (blackList.exists(object)) {
        return;
    }
    boolean notExists = set.add(object);
    if (!notExists) {
       set.remove(object)
       blackList.add(object);
    }
}
查看更多
Rolldiameter
6楼-- · 2020-06-01 02:49

You could use a HashMap<String, Integer>.

You iterate over the list and if the Hash map does not contain the string, you add it together with a value of 1.

If, on the other hand you already have the string, you simply increment the counter. Thus, the map for your string would look like this:

{"123", 2}
{"122", 1}
{"125", 1}

You would then create a new list where the value for each key is 1.

查看更多
登录 后发表回答