0

I know this question is asked before but I am really not able to find a working method I tried the method in the following link How to construct ArrayList from 2D int array? which I think is the most relevant to my case I applied the following code:

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class MonteCarloSim {

    private int[] ints;

    public MonteCarloSim(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 MonteCarloSim) {
            MonteCarloSim another = (MonteCarloSim) 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);
    }

    public static void main(String arg[]){

then I determined a 2D integer array with zero, one elements having a size of 1000*20 called IncProb1 I apply the following code to remove dublicate rows:

int[][] arrays = IncProb1;
        Set<MonteCarloSim> rows = new HashSet<MonteCarloSim>();
        for(int[] a: arrays) {
            rows.add(new MonteCarloSim(a));
        }
        for(MonteCarloSim row: rows) {
            System.out.println(row);
        }

But this doesn't work I only get one row which is [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] and this is not the solution there should be at least 300 rows not repeated. I don't know whats wrong Any help is highly appreciated Thanks in advance

1 Answer 1

0

Following method will solve your problem

public static int[][] removeDuplicateRow(int[][] testArr)
    {
        /**
         * This method will remove the duplicate ROW from 2-D array and replace with {0,0,0}
         * However i assume array rows  =5 and coloums =3 ;) You can have the solution for that I assume
         */

        HashSet<String> hashSet = new HashSet<String>();
        int[][] result = new int[5][3];
        int i = 0;
        for(int[] a : testArr)
        {
            System.out.println(Arrays.toString(a));
            System.out.println(hashSet.contains(a));
            if(!hashSet.contains(Arrays.toString(a))){
                hashSet.add(Arrays.toString(a));
                result[i] = a;
            }
            i++;
        }

        System.out.println("old array : "+Arrays.deepToString(testArr));
        System.out.println("new array : "+Arrays.deepToString(result));

        return result;
    }

Hope this helps.

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

5 Comments

Thanks but how I can convert the array back to double?
@AhmadGhaziAltarabsheh I didn't get you what you mean by 'double'. As this method returns an 2-D integer Array, you can use it to any primitive type, you need to change the array type only.
Sorry I mean how I can convert the resulting array to 2D integer array
Also How I can delet the zero rows
Returning array is a 2-D array itself. You can copy the returning array to a new array with some logic for example on the basis of sum of all the elements in a row, if sum >0 then row will be added sum =0 , then not.

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.