• Post Reply Bookmark Topic Watch Topic
  • New Topic

Searching for duplicates in a 2D array  RSS feed

 
Lonnie Wood
Greenhorn
Posts: 15
Chrome
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm wondering if someone could help me with my thought process on searching for duplicates in a 2d array.

Hopefully I'm right, but if I'm not I'd appreciate someone helping me along on my way.

I'm thinking to find a duplicate I would first set the search item to the first number in my array. I would then search for it through the array using two FOR statements. If none are found, then I would set the search to the second number in my array, and so on and so on.

I'm wanting the loop to stop and give an error after the first one is found, so I don't need to know how many duplicates there actually are.

If that's right, can anyone giv me an idea or at least a starting point?

Thanks,

Very new to arrays....
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Have you been through the Java™ tutorials about collections? There is an example of counting there, which I believe uses a Map.
 
Lonnie Wood
Greenhorn
Posts: 15
Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No not yet, but I'm heading there now.

Thanks.
 
Lonnie Wood
Greenhorn
Posts: 15
Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I checked out the tutorials but not really much on two dimensional arrays. At least when it comes to comparing them.
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no such thing as a 2D array; only an array of arrays.

Back to the counting example. Read it carefully and see how it works. Then, I suggest:
Put each value in the Map, then count it as 1. If the value is already in the Map, increment the count. So you get 1 = one occurrence, 2 = duplicated, 3 = triplicated, etc. Iterate the Set of values of the Map, and count those whose value is greater than 1. Or you could start with 0 = singleton, and you can count non-zero numbers, or add the totals together.
I presume you already know how to iterate the arrays.
 
Lonnie Wood
Greenhorn
Posts: 15
Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie,

OK, obviously new to Java. The following is what I've been asked to do, and the some of the code that I've written.

//create an array of booleans for each valid digit (1-9). Set them all to false
// iterate through your game board array looking at each digit.
// get the value of the boolean array at the index specified by the digit
// if the array has a value of false
// this is the first time we have encountered this digit
// set the boolean array to true at the index specified by the digit
// else
// this is not the first time we have encountered this digit
// we have a duplicate, so there is no winner


The above code prints the array, I understand this as it's looking at the first item in row 0, then adding one to the col number (col++), until it comes to the end of the row (array[row].length) then add one to the row (row++) Do this until it comes to the last row (row < array.length)

The area that I'm getting hung up on is "get the value of the boolean array at the index specified by the digit"

We haven't covered maps yet (as in the tutorials) so I'm stuck trying to do this in the most basic way possible. (gotta crawl, walk, run kinda thing I guess).

Any advice, tips, documents, etc. would be greatly appreciated.
Thanks.
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think what you have is an array like this, where I shall use the first letters of the possible values:

fftftftftttftftfttftftftftftfttftft

So when you have a 3, you will seek the no 3 element (ie the 4th element) of that array, which appears to be false. I suspect you are supposed to change the value to true at that stage, and if you find a true later on, you lose the game.
 
Lonnie Wood
Greenhorn
Posts: 15
Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was able to get this, but I'm trying to get the rest of the code done. As soon as I can I'll post how I did this.
Thanks for your help.
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!