473,395 Members | 1,986 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

checking array for duplicates

Hi all,

I've been stuck on this for hours so am brining it here to see if i can get any suggestions..

I have an array that i need to check to see if there are any duplicate values present. I do not need to remove the duplicates, just see if any are present. If there are duplicates, the method should return false; if there are no duplicates, the method should return true. My method keeps returning true. My code is below.

(its not a sudoku solver, but just a solution validator)

Expand|Select|Wrap|Line Numbers
  1.  private static boolean isSolution(Sudoku puzzle)
  2.   {
  3.     int count=0;
  4.     boolean solution = false;
  5.    for (Cell[] myPuzzle : puzzle)
  6.      {
  7.      for (int i=0; i<myPuzzle.length; i++)
  8.        {
  9.        for (int j=i+1; j<myPuzzle.length; j++)
  10.          {
  11.          if (myPuzzle[i] == myPuzzle[j])
  12.            count++;
  13.          }
  14.        }
  15.      }
  16.    if (count > 0)
  17.      solution= false;
  18.    else
  19.      solution = true;
  20.    return solution;
  21.   }

any insight would be greatly appreciated.
Feb 20 '10 #1
7 5074
pbrockway2
151 Expert 100+
Use a debugger or insert println() statements to check what is going on: the length of the cell arrays, and the cell values being compared. Basically you want to check that the nested for loops are doig what you think they should.

Also remember that == is not the same thing as equals(). So check exactly what myPuzzle[i] and myPuzzle[j] are. It may be that they are different Cell instances but have been assigned the same numeric value. (In which case they should count as "duplicates" even though they are different.)
Feb 20 '10 #2
ok, i insterted println statements inside the nested loop. now i am thinking the problem may lie in my code for iteration b/c whats printing is :

"Cell@165e55e
Cell@139491b
Cell@165e55e
Cell@1f04ea6" and so on.... not the actual value.


so far, my next() method only has code to iterate through the rows and columns of the puzzle. I am planning on adding the boxes code once i am sure the rows and columns are working properly.

the implementation of iterator has definitely been a little confusing for me.

this is my code:

Expand|Select|Wrap|Line Numbers
  1.   public Cell[] next()
  2.   {
  3.     Cell[] result = new Cell[puzzle.length];
  4.      // rows
  5.     if (cursor<puzzle.length)
  6.     {
  7.       Cell[] rowValue = puzzle[cursor];
  8.       cursor++;
  9.       result = rowValue;
  10.     }
  11.     // columns
  12.     else if(cursor<2*puzzle.length)
  13.     {
  14.       for (int i=0; i<puzzle.length; i++)
  15.       {
  16.         for (int j=0; j<puzzle[i].length; j++)
  17.         {
  18.           Cell[] columnValue = new Cell[j];
  19.           cursor++;
  20.           result=columnValue;
  21.  
  22.         }
  23.       }
  24.     }
  25.     return result;
  26.   }
Feb 20 '10 #3
pbrockway2
151 Expert 100+
I haven't really looked at the next() - but I think you could do more to understand the isSolution() method you posted earlier.

What is the value of myPuzzle.length for each iteration of the outer for loop? Is this what you expect?

myPuzzle[i]==myPuzzle[j] is always evaluating to false. But what are the values of the two array expressions, myPuzzle[i] and myPuzzle[j], at this point? And what are the (numeric) values of the two cells that are being compared? Is the value of "false" what you expect given the values of the array expressions and the cell contents?

The cryptic "Cell@165e55e" arises because you have not overridden toString() method for the Cell class. (See the API docs).
Feb 20 '10 #4
after i posted that last msg, i realized the problem within my isSolution and fixed it (needed to call myPuzzle[i].getValue(), etc, to get numerical values of the cells). But i am having a difficult time understanding the Iterator and how to get it to loop through the columns. my code to iterate over the rows seems to be correct. i've tried reading more about iterators, but am still just not sure how to get it to iterate over only the columns.
Feb 21 '10 #5
pbrockway2
151 Expert 100+
Is next() supposed to return an array of cells representing the (cursor-puzzle.length)-th column?

If so
- create result as an array of the right size
- in a single for loop go through the rows (ie puzzle[???]) and copy the appropriate cell into the right place in the result array
- return the result array

For example, suppose we wanted the 3rd column (when cursor puzzle.length+2). You would go through each row and copy the third element into the return array:

Expand|Select|Wrap|Line Numbers
  1. row 0  x x * x x x ...    result =[*]
  2. row 1  x x & x x x ...    result = [* &]
  3. row 2  x x ! x x x ...    result = [* & !]
  4. row 3  x x % x x x ...    result = etc
  5.  
Feb 21 '10 #6
thank you for helping me out here. i am not sure why this is giving me so much trouble.

next() is supposed to return 3 different one-dimensional arrays. one containing the rows, one containing the columns, and one containing the sub-boxes of the puzzle (which i have yet to try tackling). though, currently, it only seems to return an array for the rows.

i feel pretty lost here. I understand what you are saying, and i know which values i need to copy over into the columns array, i just am not exactly sure how to pull out the particular ones i need.

am i getting warmer at all?

Expand|Select|Wrap|Line Numbers
  1.  public Cell[] next()
  2.   {
  3.     Cell[] result = new Cell[3*puzzle.length];
  4.      // rows
  5.     if (cursor<puzzle.length)
  6.     {
  7.       Cell[] rowValue = puzzle[cursor];
  8.       cursor++;
  9.       result = rowValue;
  10.     }
  11.  
  12.     // columns
  13.     else if(cursor<2*puzzle.length)
  14.     {
  15.       for (int i=0; i<puzzle.length; i++)
  16.       {
  17.         Cell[] columnValue = puzzle[cursor];
  18.         cursor+=puzzle.length+i;
  19.         result=columnValue;
  20.  
  21.  
  22.       }
  23.     }
  24.     //boxces code here
  25.  
  26.     return result;
  27.   }
Feb 21 '10 #7
pbrockway2
151 Expert 100+
What happens when you run your code?

> Cell[] columnValue = puzzle[cursor];

I would suppose that this line yields an ArrayIndexOutOfBounds exception. The puzzle array does not go as far as cursor once cursor exceeds puzzle.length.

Instead of the three lines you have in the for loop, try this:

(1) Get the relevant row not column.

Expand|Select|Wrap|Line Numbers
  1.     Cell[] row = puzzle[???];
  2.  
Think about how the index should be expressed given that you want to work through each row in order.

(2) From each row extract one of the elements and place it in the appropriate place in the result array.

Expand|Select|Wrap|Line Numbers
  1. result[???a] = row[???b]
  2.  
Once again you have to figure out the array indices. Look at the diagram I posted before. ???a starts at zero and works its way along the result array: it is related to the loop index i.

???b is the element of the row that needs to be added as you buld up the column array. It is constant (ie the same each time around the loop). It is related to the cursor value.

-----

All my comments are presume that I understand what you are trying to do with next(). Your comment that "next() is supposed to return 3 different one-dimensional arrays" doesn't really help because next() is declared to return a single array of cells. What I'm assuming is that next() is supposed to iterate over all 27 "groups". Called repeatedly it will return the nine rows (each as an array), then the 9 columns, then the nine boxes.

-----------------------

At this point you should be able to get the rows and columns part going. And run your isSolution() with some real data. If the runtime behaviour isn't what you expect then post your code and say what happens.
Feb 21 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Michelle | last post by:
hi, i have created an array from recordset containing user names eg. (davidp, davidp, evenf, patricka, rebeccah) which i have sorted in alphabetical order, but i need to identify duplicates...
18
by: Dan | last post by:
hello, I would to know if it is possible to delete an instance in an array, The following does not allow me to do a delete. I am trying to find and delete the duplicate in an array, thanks ...
5
by: Jermin | last post by:
Hi, I have a database composed of a single table composed of 2 columns, an auto-numbered ID column and a column which contains 30 million random numbers. All I want to Access to do is check the...
14
by: ak | last post by:
Is it possible to find repeated(duplicate) element in an array in single loop ? AK
4
by: Mokita | last post by:
Hello, I am working with Taverna to build a workflow. Taverna has a beanshell where I can program in java. I am having some problems in writing a script, where I want to eliminate the duplicates...
1
by: AndyB | last post by:
I have found a lot of material on removing duplicates from a list, but I am trying to find the most efficient way to just check for the existence of duplicates in a list. Here is the best I have...
5
by: fluk | last post by:
Hi Guys, I hope someone can help me with this, because i'm getting crazy to find a good way to do that! This is what I got by querying a db. $arr1 = array("site", "description", "area1" ,...
7
by: john.cole | last post by:
I have searched all the groups I can, and I still haven't been able to come up the solution I need. I have the following problem. In my form named sbfrmSpoolList, I am entering a job, spool and...
2
by: chemlight | last post by:
I'm trying to figure out how to order an array based on the number of duplicates, and then remove all duplicates. This is my code right now: foreach($acID as $searchterms){ $results =...
3
Thekid
by: Thekid | last post by:
I'm trying to figure out a way to find if there are duplicates in an array. My idea was to take the array as 'a' and make a second array as 'b' and remove the duplicates from 'b' using 'set' and then...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.