2

how would I go about checking a duplicate for columns and rows and return true or false depending if there's duplicates. For example

1 2 3

3 1 2

2 3 1

Would return true because no duplicates, but..

1 2 2

3 2 3

2 1 1

would return false because there is a duplicate in column 2 {2, 2, 1}.

How would I go about checking for if there are in duplicates in the rows and then checking if there are duplicates in the columns?

So far I have only the following:

public static boolean hasDuplicates(int [][] inArray)
{
   for (int row = 0; row < inArray.length; row++)
   {
      int rowCheck = inArray[row][inArray.length];
      for (int col = 0; col < inArray[row].length; col++)
      {

      }
   }
   return false;
}

So I need to take in an array and check if there are any duplicates among the columns or rows. Any pointers? Thank you!

NOTE: This cannot be done with outside methods

3
  • 3
    how is 3-2-3 not a "duplicate" in your first example? It's a little unclear what you're asking. Commented Feb 21, 2014 at 0:00
  • @BrianRoach my apologies! The third row should be 2-3-1, edited! Commented Feb 21, 2014 at 0:06
  • You may find your answer in this (exact same) question: stackoverflow.com/questions/6122315/… Commented Feb 21, 2014 at 0:33

4 Answers 4

3

A more compact way of achieving this task is to add all the values to a Set and then compare the number of unique elements in the Set to the original row size using guava.

public static boolean hasDuplicates(int [][] inArray) {
    for (int row = 0; row < inArray.length; row++) {
        int curRow = inArray[row];
        Set set = Sets.newHashSet(Arrays.asList(curRow));
        if (set.size() < curRow.length) {
            return true;
        }
    }
    return false;
}
Sign up to request clarification or add additional context in comments.

1 Comment

this is for a program where I'm not allowed to use sets or lists. Sorry, I should have mentioned that above.
1

Go through each value in the row. For every value, check and see if any of the values after that value are the same. If the value is the same, return true (you've found a duplicate). If none of the values are the same, increment your index and do the same for the next row. Every row will take at most n(n+1)/2 comparisions which isn't wonderful. So if n is the number of columns and m in the number of rows, this will run, worst case m(n(n+1)/2) times.

Here is an example of how it would work for the rows:

/**
 * Return flag indicating if there are duplicates in the rows of the 2D array
 * 
 * @return true if a row has duplicates, else false
 */
public boolean hasDuplicatesInRows(int[][] inArray)
{
    for (int row = 0; row < inArray.length; row++)
    {
        for (int col = 0; col < inArray[row].length; col++)
        {
            int num = inArray[row][col];
            for (int otherCol = col + 1; otherCol < inArray.length; otherCol++)
            {
                if (num == inArray[row][otherCol])
                {
                    return true;
                }
            }
        }
    }

    return false;
}

That would be pretty easy to extend to do columns as well. I will leave that for you to do though.

If you used an efficient sorting method and sorted all the rows, you could just go down the line and see if any value was equal to the value after it. If it is, return true, else return false. This would be more efficient if you had a large data set.

6 Comments

Sorry, I should have mentioned above that I can't use any outside methods. Thanks for your help though, wish I could go this route
done. Do you have any pointers for doing this without. I understand the concept I just don't know how to achieve it
@user3335070 I fixed my answer to not use Collections. It's almost the same as before, but it does a check for each value in the row against the other values. I'm not convinced this is the most efficient, but it does work and it's simple.
Thank you so much, I can't express how grateful I am.. I've been working on this since 11am and I've been in such a block. Thank you. To get columns would I then switch them around so the first for loop would be for (int col = 0; col < inArray[0].length; col++) then the row on the inside? Then substitute row for the otherCol?
Aweomse, thanks so much. You definitely saved me from pulling an all nighter!
|
1

Let's say you create a method that operates on a single-dimensional array. You break the problem down into extracting strips of numbers from your 2d array into a 1d array. It's signature might look like boolean containsDupes(int[] strip) { ... }.

There are a couple of approaches in that method that would make it easier to solve. One would be to sort that array so the dupes are next to each other. Another would be to populate a HashSet with each value and compare the size of the Set with the length of your array.

Comments

1

for rows, try following this sort of procedure:

boolean dup = false;
for (int k = 0; k < inArray[0].length){ //loop through columns
  for (i = 0; i < inArray.length-1; i++) {
    for (int j = i; j < inArray.length; j++){
      if (inArray[k][i] == inArray[k][j]){
        dup = true;
        break;
      }
    }
  }
}

so, you're starting at the first element, then scanning from element 2 to n (ie number of columns). If a match is found, then you're setting the boolean to true. If no match, then i is incremented and the inner for loop is scanning from element 3 to n.

Follow a similar procedure for the columns, and you're done!

2 Comments

so would for (i = 0; i < n-1; i++) be for (i = 0; i < inArray.length-1; i++) and then the next be for(int j = i; j < inArray[i].length; j++)? Thank you for your help @h_k
Yes. I had "n" in there originally because I started making my answer more generalized. I'll throw that into my answer.

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.