Java - Listing the index of duplicate values in an int array
Solution 1
I think you should use a List
to record the indexs
List<Integer> indexs =new ArrayList<Integer>();
for (p = 0; p < array.length; p++) {
if(array[p]==search) {
indexs.add(p);
}
}
if(p.length()>0){
//print the result
}
Solution 2
How about just two for loops?
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length; j++) {
if (array[i] == array[j]) {
System.out.println("Duplicate - " + array[i] + " found at index " + i + " and " + j);
}
}
}
Solution 3
No need for Hash tables, lists or whatever, you can do this very easily as so:
int [] array = { 0, 7, 9, 1, 5, 8, 7, 4, 7, 3};
int pointer=0;
int currNumber;
while(pointer<array.length)
{
currNumber=array[pointer];
for(int i=0;i<array.length;i++){
if(currNumber==array[i] && i>pointer){
System.out.println("Duplicate for "+currNumber +" in " +i);
break;
}
}
pointer++;
}
It will print all duplicates for all the numbers in the array.
Duplicate for 7 in 6
Duplicate for 7 in 8
Obviously, you'd probably have to concatenate a string and display it at the end of the loop by calling outputResults.setText()
user1462300
Updated on June 04, 2022Comments
-
user1462300 almost 2 years
I have to locate and list all duplicate index values in array.
Example: int[] array = { 0, 7, 9, 1, 5, 8, 7, 4, 7, 3};
7 is located in three different locations at index 1, 6, and 8. How would I go about modifying my existing code in order to have outputResults.setText() show the location of the duplicate values? outputResults.setText() is JTextField if that helps.
String tmp1 = getNumbers.getText(); try { int search = Integer.parseInt(tmp1); for (p = 0; p < array.length; p++) { if(array[p]==search) { b = true; index = p; } } if(b==true) outputResults.setText(search + " was in the following fields of the array " + index); else throw new NumberNotFoundException("Your number was not found."); } catch (NumberFormatException ex) { JOptionPane.showMessageDialog(getContentPane(), "You can only search for integers."); } catch (NumberNotFoundException ex) { JOptionPane.showMessageDialog(getContentPane(), ex.getMessage()); }
At it's current state, it will only list the last time the duplicate number was located which would be index 8 based on my example. The list of numbers in the array is inputted by the user, and I'm not allowed to sort the values. My original guess was to create a nested loop and whenever it located an duplicated number, add p (current index it's searching) to a new array. I would then list the full array in outputResults.setText() but it gave several warnings and errors when I tried.
The full code can be found here if needed: http://pastebin.com/R7rfWAv0 And yes, the full program is a mess but it gets the job done and I was having such a headache with it. Also note, in the full program, the professor asked us to throw an exception if a duplicate value was detected as extra credit. I did it, but I commented it out to finish the original assignment so please disregard it.
-
user1462300 almost 12 yearsI'm sorry but I don't know what a HashMap is. We never went over anything related to it in class. Would you be willing to explain it a little? I'll research into it in the meantime.
-
user1462300 almost 12 yearsWe haven't gone over List yet in class but after a reading about it in orcales, it seems like it can get the job done. The only problem is that eclipse is telling me "The type List is not generic; it can not be parameterized with arguments <Integers>". I'll look further into it, thanks for the tip!
-
plucury almost 12 years@user1462300 Just use
<Integer>
is OK.<Integers>
is invalid.see java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf for more detail. -
user1462300 almost 12 yearsOh wow, cannot believe I forgot about +=. That actually worked perfectly! And yea, I know there's several ways to achieve the results I wanted, but like I mentioned before, when you work on something for a long period of time, you tend to forget the basics and simple methods of doing things. Thank you so much!
-
Michael almost 12 yearsdocs.oracle.com/javase/6/docs/api/java/util/HashMap.html Basically, it's a hashtable but implemented using the Map Collection interface.
-
Donnie almost 12 yearsIf this helped solve your issue please mark it as accepted. The tick mark under the number of up votes.
-
karlihnos about 6 yearsThats wrong! you are always evaluating the same position. E.g. array[0] == array[0]
-
Gavin about 6 years@karlihnos Pretend array.length = 3. The loops are nested. It enters first loop at i = 0. Then enters second loop at j = 0. Then i=0, j=1; i=0, j=2. Now j=3 !< array.length, so exit second loop. First loop increments i. i=1, j=0; i=1, j=1, etc.
-
karlihnos about 6 yearsTaking the example from the op: int[] array = { 0, 7, 9, 1, 5, 8, 7, 4, 7, 3}. He says: 7 is repeated at postiitons 1,6 and 8. In your code you evaluate array[0]==array[0], after j is done, it continues comparing array[1] and j will be somewhen arrays[1], it means again array[1]=array[1]. So basically, you are comparing each number with its self