0

I have a 2D int array, i want to remove duplicate rows for example

30,40,50

50,30,40

30,40,50

in above example 2nd & 3rd row is duplicate of 1st row.

I know ArrayList can dynamically grow and sink which is useful class for this concept but how we convert int[][] into ArrayList.

1
  • Does each line always have the same amount of values? Or is that variable as well? Commented May 28, 2012 at 11:10

5 Answers 5

2

If you want all unique numbers straight away then you could use Set<Integer> directly

30,40,50
50,30,40
30,40,50

in above example 2nd & 3rd row is duplicate of 1st row

if you see all 3 rows same then You could directly use Set<Integer>

Sign up to request clarification or add additional context in comments.

3 Comments

I don't think that would work out for the problem at hand. The op wants to discard lines that have exactly the same points as a line previously entered, not remove all duplicate points (I think).
@pcalcao OP sees all 3 line same, see the quoted block
Well, to eliminate duplicate lines what you'll want is a Set<Set<Integer>>.
2

First, you should create a Class to hold you data which override public boolean equals(Object obj) and public int hashCode() to indicate equality of the data.

public class Row {

    private int[] ints;

    public Row(int[] ints) {
        this.ints = ints.clone();
        Arrays.sort(this.ints);
    }

    @Override
    public int hashCode() {
        return Arrays.hashCode(ints);
    }

    @Override
    public boolean equals(Object obj) {

        if(obj instanceof Row) {
            Row another = (Row) obj;
            int[] original = Arrays.copyOf(another.ints, another.ints.length);
            return Arrays.equals(ints, original);
        } else {
            return false;
        }
    }

    @Override
    public String toString() {
        return Arrays.toString(ints);
    }

}

Test Case

public class Test {

    public static void main(String[] args) {
        int[][] arrays = new int[][]{{30,40,50}, {50,30,40}, {30,40,50}, {10, 20, 30}};
        Set<Row> rows = new HashSet<Row>();
        for(int[] a: arrays) {
            rows.add(new Row(a));
        }
        for(Row row: rows) {
            System.out.println(row);
        }
    }
}

Output

[10, 20, 30]
[30, 40, 50]

1 Comment

This was also my first thought, but you're just re-implementing a set here ;)
0
// lets create table
int[][] int2d = { 
        { 1, 2 }, 
        { 2, 1 }, 
        { 30, 40, 50 }, 
        { 50, 30, 40 },
        { 30, 40, 50 }, };

// lets sort content of each row for
for (int[] row : int2d)
    Arrays.sort(row);
// lets see how table looks likne now
System.out.println(Arrays.deepToString(int2d));

// this will help set do decide if element is already in
Comparator<int[]> comparator = new Comparator<int[]>() {
    public int compare(int[] o1, int[] o2) {
        return Arrays.toString(o1).compareTo(Arrays.toString(o2));
    }
};

// we create set and give him comparator (via constructor)
Set<int[]> set = new TreeSet<int[]>(comparator);
// now lets try to put every row of table in set
for (int[] row : int2d)
    set.add(row);

// NOW, GREAT TEST
System.out.println("============");
for (int[] row : set)
    System.out.println(Arrays.toString(row));

out:

[[1, 2], [1, 2], [30, 40, 50], [30, 40, 50], [30, 40, 50]]
============
[1, 2]
[30, 40, 50]

1 Comment

This was also my first thought, but you're just re-implementing a set here ;) (EDIT: this was meant for the other post :( sorry)
0

This is a minimal code that will eliminate the duplicates as you have defined them:

final Integer[][] int2d = { { 30, 40, 50 }, { 50, 30, 40 }, { 30, 40, 50 }, };
final Set<Set<Integer>> r = new LinkedHashSet<Set<Integer>>();
for (Integer[] row : int2d) r.add(new LinkedHashSet<Integer>(Arrays.asList(row)));
System.out.println(r);

4 Comments

Thanks!for providing such minimal code it is working nicely. Given: HashMap<String,String> map; I can create an array from this map with this simple loop: String[][] array = new String[map.size()][2]; int count = 0; for(Map.Entry<String,String> entry : map.entrySet()){ array[count][0] = entry.getKey(); array[count][1] = entry.getValue(); count++; }
Given: HashMap<String,String> map; I can create an array from this map with this simple loop: String[][] array = new String[map.size()][2]; int count = 0; for(Map.Entry<String,String> entry : map.entrySet()){ array[count][0] = entry.getKey(); array[count][1] = entry.getValue(); count++; }
i can do from HashMap to 2D string but how we will create a 2D Integer array from LinkedHashSet as generated by using your suggested code
as well as how we can break arr[n][3] into two array first[n][2] and second[n][1] so that persist correspondents also with arr[n][3].
0

If you use the persistent collections from Functional Java, the solution is quite simple.

(Note: List and Set I am referring to below are from Functional Java, and not from standard library.)


Let us say you are storing your data in a variable named xss of type List<List<Integer>>.

What you want to do can be described as follows:

Deduplicate xss with the criterion that the two rows that contain same elements are to be considered equal.

  • List provides a method nub for deduplication.
  • Whether or not the two Lists contain same elements can be found out by converting them to Sets and then comparing them for equality.
  • The above criterion can be passed as a Equal instance.

Code:

xss.nub(new Equal<List<Integer>>() {
  public boolean eq(List<Integer> xs1, List<Integer> xs2) {
    Set<Integer> s1 = Set.iterableSet(Ord.listOrd(Ord.intOrd), xs1);
    Set<Integer> s2 = Set.iterableSet(Ord.listord(Ord.intOrd), xs2);
    return Equal.setEqual(Equal.intEqual).eq(s1, s2);
  }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.