Home > database >  How to remove duplicates from Set of list in Java
How to remove duplicates from Set of list in Java

Time:10-29

I have a set of lists that I want to remove duplicates from no matter the order of elements in each list, as the following :

I have this as input [[-1,-1,2],[0,-1,1],[1,-1,0],[2,-1,-1],[-1,2,-1],[-1,1,0],[0,1,-1],[-1,0,1],[1,0,-1]]

When I use Set<Set> to refine my elements it does partially the work but I get [[1,-1,0],[-1,2]] which it is logic because the inner Set refines the duplicates for [-1,-1,2].

When I tried to use Set<List> I couldn't refine my elements which gets me this [[-1,-1,2],[0,-1,1],[1,-1,0],[2,-1,-1],[-1,2,-1],[-1,1,0],[0,1,-1],[-1,0,1],[1,0,-1]]

So how I can manage to refine the duplicates and keep my resulting triplets intact?

Thank you in advance.

CodePudding user response:

I think you could use sorting to make using a Set and a List work as you specify:

import java.util.Arrays;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

class Main {
    
    public static void main(String[] args) {
        int[][] arrayWithDuplicates = new int[][] { { -1, -1, 2 }, { 0, -1, 1 }, { 1, -1, 0 }, { 2, -1, -1 },
                { -1, 2, -1 }, { -1, 1, 0 }, { 0, 1, -1 }, { -1, 0, 1 }, { 1, 0, -1 } };
        System.out.printf("arrayWithDuplicates = %s%n", Arrays.deepToString(arrayWithDuplicates));
        int[][] arrayWithoutDuplicates = getArrayWithoutDuplicates(arrayWithDuplicates);
        System.out.printf("arrayWithoutDuplicates = %s%n", Arrays.deepToString(arrayWithoutDuplicates));
    }

    public static int[][] getArrayWithoutDuplicates(int[][] array) {
        List<int[]> listWithoutDuplicates = new ArrayList<>();
        Set<List<Integer>> seenSubLists = new HashSet<>();
        for (int[] ints : array) {
            List<Integer> sortedInts = Arrays.stream(ints).boxed().sorted().collect(Collectors.toList());
            if (!seenSubLists.contains(sortedInts)) {
                listWithoutDuplicates.add(ints);
                seenSubLists.add(sortedInts);
            }
        }
        return listWithoutDuplicates.toArray(new int[listWithoutDuplicates.size()][]);
    }

}

Output:

arrayWithDuplicates = [[-1, -1, 2], [0, -1, 1], [1, -1, 0], [2, -1, -1], [-1, 2, -1], [-1, 1, 0], [0, 1, -1], [-1, 0, 1], [1, 0, -1]]
arrayWithoutDuplicates = [[-1, -1, 2], [0, -1, 1]]

CodePudding user response:

    final Set<List<Integer>> sortedLists = new HashSet<>();
    Set<List<Integer>> newLists = lists.stream()
            .map(integers -> {
                List<Integer> sorted = integers.stream().sorted().collect(Collectors.toList());
                if (sortedLists.contains(sorted)) {
                    return null;
                }
                sortedLists.add(sorted);
                return integers;
            })
            .filter(Objects::nonNull)
            .collect(Collectors.toSet());

CodePudding user response:

You can create a class to represent your set elements and give them the behaviour you want. That is, two elements are equal if they contain the same integers regardless of order.

import java.util.Arrays;
public class IntList extends Object {
    // I will keep the original array but you can just sort it in place if that makes sense
    private int[] array; // The orignal array
    private int[] sortedArray; // Sorted copy of the original array
    public IntList( int[] array ) {
        this.array = array;
        this.sortedArray = array.clone();
        Arrays.sort( this.sortedArray );
    }
    @Override
    public boolean equals( Object o ) {
        // This object is equal to another if they are:
        //   the same instance or instances of this class with equal sorted arrays
        boolean result;
        if ( o == this ) {
            result = true;
        } else {
            if ( ! ( o instanceof IntList ) ) {
                result = false;
            } else {
                IntList other = ( IntList ) o;
                result = Arrays.equals( this.sortedArray, other.sortedArray );
            }
        }
        return result;
    }
    @Override
    public int hashCode() {
        // Used by HashSet
        return Arrays.hashCode( this.sortedArray );
    }
    @Override
    public String toString() {
        return Arrays.toString( this.sortedArray );
    }
}

Then you can construct a Set with elements of this class:

import java.util.Arrays;
import java.util.Set;
import java.util.HashSet;

public class main {
    public static void main(String[] args) {
        int[][] input = new int[][] { { -1, -1, 2 }, { 0, -1, 1 }, { 1, -1, 0 }, { 2, -1, -1 },
                { -1, 2, -1 }, { -1, 1, 0 }, { 0, 1, -1 }, { -1, 0, 1 }, { 1, 0, -1 } };

        System.out.printf("input = %s%n", Arrays.deepToString(input));

        Set<IntList> set = new HashSet<IntList>();
        for( int[] currIntArray: input ) {
            IntList list = new IntList( currIntArray );
            set.add( list );

        }
        System.out.printf( "output = %s%n", set.toString());
    }
}

Result

input = [[-1, -1, 2], [0, -1, 1], [1, -1, 0], [2, -1, -1], [-1, 2, -1], [-1, 1, 0], [0, 1, -1], [-1, 0, 1], [1, 0, -1]]
output = [[-1, -1, 2], [0, -1, 1]]

The way you do this really depends on the larger context of your problem domain. I think it's unlikely that you really want a public class called IntList but you might contain it inside your own implementation of Set, or somewhere else in your model.

  • Related