简体   繁体   English

如何从 ArrayList 的 ArrayList 中删除重复的 ArrayList

[英]How to remove duplicates ArrayList from an ArrayList of ArrayList

Problem: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0?问题:给定一个包含 n 个整数的数组 S,S 中是否存在元素 a、b、c 使得 a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.在数组中找到所有唯一的三元组,其总和为零。

My Code:我的代码:

public class Solution {
    public ArrayList<ArrayList<Integer>> threeSum(ArrayList<Integer> A) {
        ArrayList<ArrayList<Integer>> C = new  ArrayList<ArrayList<Integer>>();
        int n = A.size();
        for(int i =0; i<n-2; i++){
            for(int j=i+1; j<n-1; j++){
                for(int k=j+1; k< n; k++){
                    int sum = A.get(i)+A.get(j)+A.get(k);
                    if(sum == 0){
                        ArrayList<Integer> temp = new ArrayList<Integer>();
                        temp.add(A.get(i));
                        temp.add(A.get(j));
                        temp.add(A.get(k));
                        C.add(temp);
                    }
                }
            }

        }
      return C;
    }
}

So C may contains repeated Arraylist, and my aim is to remove repeated Arraylist from C所以C可能包含重复的 Arraylist,我的目标是从 Z0D61F8816C2CB92B12F24A568025B749E5Z 中删除重复的 Arraylist

Example: C = [-5 1 4 ] [-5 1 4 ] [-5 1 4 ] [-5 4 1 ] [-4 0 4 ] [-4 0 4 ]示例:C = [-5 1 4 ] [-5 1 4 ] [-5 1 4 ] [-5 4 1 ] [-4 0 4 ] [-4 0 4 ]
My aim to get = [-5 1 4 ] [-5 4 1 ] [-4 0 4 ]我的目标是 = [-5 1 4 ] [-5 4 1 ] [-4 0 4 ]

Please suggest me some ways to do some operation on C so that I can do it.请建议我一些方法对 C 进行一些操作,以便我可以做到。

The equals method of AbstractList (which ArrayList extends) is defined so that two lists are equal if they contain the same elements in the same order. AbstractList 的 equals 方法(ArrayList 扩展)被定义为如果两个列表包含相同顺序的相同元素,则它们是相等的。 The simplest way then would be to get the distinct lists from a stream:最简单的方法是从 stream 中获取不同的列表:

List<List<Integer>> list = new ArrayList<>();
list.add(Arrays.asList(-5, 1, 4));
list.add(Arrays.asList(-5, 1, 4));
list.add(Arrays.asList(-5, 4, 1));
list.add(Arrays.asList(-4, 0, 4));
list.add(Arrays.asList(-4, 0, 4));
list.add(Arrays.asList(-4, 0, 4));

List<List<Integer>> distinctLists = list.stream().distinct().collect(Collectors.toList());

System.out.println(distinctLists); // prints [[-5, 1, 4], [-5, 4, 1], [-4, 0, 4]]
As given in previous answer, create a list of list
List<List<Integer>> listToBeUpdated = new ArrayList<>();
list.add(Arrays.asList(-5, 1, 4));
list.add(Arrays.asList(-5, 1, 4));
list.add(Arrays.asList(-5, 4, 1));
list.add(Arrays.asList(-4, 0, 4));
list.add(Arrays.asList(-4, 0, 4));
list.add(Arrays.asList(-4, 0, 4)); 

Then, You can achieve this using Set(as set do not allow duplicates).然后,您可以使用 Set 实现此目的(因为 set 不允许重复)。 in Java 8:在 Java 8 中:

    listToBeUpdated.stream().flatMap(List::stream).collect(Collectors.toSet())

Or you can simply:或者您可以简单地:

    public List<Integer> removeDuplicatesFromList(List<List<Integer>> listToBeUpdated) {
        Set<Integer> uniqueList = new HashSet<Integer>();    //Use TreeSet<Integer>, for sorting order or LinkedHashSet<Integer> for insertion order
        for(List<Integer> anInt: list) {
            uniques.addAll(anInt);
        }
        return new ArrayList<>(uniqueList);   // If you want to return list
    }

Hope it helps !

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM