Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
959 views
in Technique[技术] by (71.8m points)

java - How do i find and count duplicates in a 2 dimensional array?

Hi I am trying to go through a two-dimensional array (specifically a 4x4 array) and both find any numbers that repeat, then count the number of times that the number repeats. So far I have 4 for loops that work however do more than what i really want.

int counter1 =1;
    String huh="";

    for (int x = 0; x< dataTable.length; x++)
    {
        for (int y=0; y< dataTable.length; y++)
        {
            for (int z = 0; z< dataTable.length; z++)
            {
                for (int a=0; a< dataTable.length; a++)
                {
                    if ( x != z && x !=a && y != z && y !=a)
                    {
                        if (dataTable[x][y] == dataTable[z][a])
                        {
                        counter1++;
                        }
                    }   
                }
            }
        if (counter1 > 1)
        {
        huh += ("
 " + dataTable[x][y] + " repeats " + counter1 + " times!");
        }
        counter1=1;
        }
    }

Basically this works in the sense that it compares every number in my array with every other number including itself (but the if statement keeps it from counting itself). Basically i need the output to state something simple like

The number 3 repeats 3 times

However with the way that my setup works it would add to the string that same statement each time it compared the number 3 in each of its places in my array. So is my method correct at all and only needs some tweaking? or is it wrong altogether and i need something totally different? I am only in a beginner programming class at my college so we only know the basics of java so far like arrays, loops, and a few other things.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Just convert this array into a Map<Integer, Integer> and then print it out, like this:

    public static void main(String[] args) throws Exception {
        final int[][] dataTable = new int[][] {
                new int[] {0, 1, 2, 1},
                new int[] {0, 1, 3, 1},
                new int[] {0, 1, 2, 2},
                new int[] {0, 1, 2, 0}
        };

        final Map<Integer, Integer> map = new HashMap<Integer, Integer> ();
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                final int value = dataTable[i][j];
                final Integer currentCount = map.get(value);
                final Integer newCount;
                if (currentCount == null) {
                    newCount = 1;
                }
                else {
                    newCount = currentCount + 1;
                }

                map.put (value, newCount);
            }
        }

        for (final Map.Entry<Integer, Integer> entry : map.entrySet()) {
            System.out.println(String.format ("The number %d repeats %d times", entry.getKey(), entry.getValue()));
        }
    }   

Here you can find the results.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...