Jake Brooks
Jake Brooks

Reputation: 41

Finding Duplicate Array Elements

I've been struggling to create a function to essentially find all the indices of duplicate elements in a multi-dimensional array(unsorted), in this case a 5x5 array, and then using the indices found changing the parallel elements in a score array. But only find duplicates within columns and not comparatively to the other columns in the array Here is what I've done so far, with research online. The main problem with this code is that it will find all the duplicate elements but not the originals. For example: if the array holds the elements: {{"a","a","a"},{"b","b","b"},{"a","c","a"}}, then it should change the parallel score array to: {{0,1,0},{1,1,1},{0,1,0}}. But instead it only recognizes the last row and top the top row's duplicates.

Code:

public static void findDuplicates(String a[][])
{
    System.out.println("*Duplicates*");
    Set set = new HashSet();
    for(int j = 0; j<a.length; j++)
    {
        for(int i=0; i < a[0].length; i++)
        {
            if(!set.contains(a[i][j]))
            {
                set.add(a[i][j]);
            }
            else
            {
                System.out.println("Duplicate string found at index " + i + "," + j);
                scores[i][j] -= scores[i][j];
            }

        }
        set = new HashSet();
    }
}

I know my explanation is a bit complicated, but hopefully it is understandable enough. Thanks, Jake.

Upvotes: 1

Views: 1892

Answers (1)

cletus
cletus

Reputation: 625087

Your logic is incorrect. Your outer loop is j and inner loop is i but you're doing:

set.add(a[i][j]);

It should be the other way around:

set.add(a[j][i]);

Technically you could get an out of bounds exception if the array isn't NxN. But you can state that as a precondition.

For some reason you're also setting to 0 with:

scores[i][j] -= scores[i][j];

Why not just:

scores[i][j] = 0;

But to find duplicates within columns:

public static void findDuplicates(String a[][]) {
  for (int col=0; col<a[0].length; col++) {
    Map<String, Integer> values = new HashMap<String, Integer>();
    for (int row=0; row<a.length; row++) {
      Integer current = values.put(a[row][col], row);
      if (current != null) {
        scores[row][col] = 0;
        scores[current][col] = 0;
      }
    }
  }
}

How does this work?

  • I've renamed the loop variables to row and col. There's no reason to use i and j when row and col are far more descriptive;
  • Like you I assume the input array is correct as a precondition. It can be NxM (rather than just NxN) however;
  • I use a Map to store the index of each value. Map.put() returns the old value if key is already in the Map. If that's the case you've found a duplicate;
  • The current (row,col) and (current,col) are set to 0. Why subtract the score from itself rather than simply setting to 0?
  • if the value "a" is found 3+ times in a column then scores[current][col] will be set to 0 more than once, which is unnecessary but not harmful and makes for simpler code.
  • I've declared the Map using generics. This is useful and advisable. It says the Map has String keys and Integer values, which saves some casting;
  • It also uses auto-boxing and auto-unboxing to convert an int (the loop variable) to and from the wrapper class Integer.

Upvotes: 3

Related Questions