Results 1 to 4 of 4
 09042009, 09:33 AM #1Member
 Join Date
 Sep 2009
 Posts
 14
 Rep Power
 0
FInd the no. of duplicates in an array
I needto find the no. of duplicates in the array. There seems to be something wrong with the loops. Can anyone see where I am wrong in looping.
Thanks. Your help is greatly appreciated:
public class duplicate {
int temp, true_count;
int ar[] = { 1, 2, 3, 4, 1 };
int count;
public void find() {
for (int i = 0; i < ar.length  1; i++) {
temp = ar[i];
for (int j = i; j <= ar.length1 ; j++) {
if (temp == ar[j]) {
count += 1;
}
}
}
}
public static void main(String args[]) {
duplicate dup = new duplicate();
dup.find();
System.out.println("The No of duplicates is :" + dup.count);
}
}
 09042009, 09:50 AM #2Senior Member
 Join Date
 Aug 2009
 Posts
 2,388
 Rep Power
 11
This has been done many times and the trick is to use a HashMap.
You just go through the array once and at each index you get the current value in the array and get it from the hashmap using itself as the key. If you get null from the map then the key is not there and you put it in setting it's value to 1. Otherwise increment it's value.
At the end of that loop you'll have a Map with the keys being the array elements and the values being the number of times each element appears in the array.
 09042009, 10:08 AM #3Member
 Join Date
 Sep 2009
 Posts
 14
 Rep Power
 0
DO You mean to say it is not possible to do it with arrays: I execute the code but it gives correct results in some cases amd wrong results in most of the cases:
Here is my code and various results:
public class duplicate {
int temp;
int ar[] = { 2, 2, 3, 4, 2 };
int count;
private void find() {
for (int i = 0; i < ar.length  1; i++) {
temp = ar[i];
for (int j = i+1; j <= ar.length 1; j++) {
if (temp == ar[j]) {
count += 1;
}
}
}
}
public static void main(String args[]) {
duplicate dup = new duplicate();
dup.find();
System.out.println("The No of duplicates is :" + dup.count);
}
}
OutPUT: The No of duplicates is :3 // Correct
If int ar[] = { 1, 2, 3, 4, 2 }; then output is:
The No of duplicates is :1// wrong should be 2
if int ar[] = { 1, 2, 3, 4, 1 }; then output is:
The No of duplicates is :1// wrong should be 2
if int ar[] = { 1, 1, 3, 4, 1 }; then output is:
The No of duplicates is :3//right
if int ar[] = { 1, 2, 3, 3, 1 } then output is:
The No of duplicates is :2// right
if int ar[] = { 3, 2, 4, 3, 1 }; then output is:
The No of duplicates is :1// should be 2
And so on......
Most IMpt:
If int ar[] = { 4, 4, 4, 4, 4 };
Then Output is:
The No of duplicates is :10Last edited by singularity; 09042009 at 10:10 AM. Reason: I pasted some more code that was not needed.
 09042009, 10:25 AM #4Senior Member
 Join Date
 Aug 2009
 Posts
 2,388
 Rep Power
 11
Similar Threads

find the greatest and lowest number in 2D array
By le_albina@hotmail.com in forum New To JavaReplies: 2Last Post: 03312009, 12:09 AM 
To find the Maximum and Minimum in an Array of Strings
By luscious in forum JavaServer Pages (JSP) and JSTLReplies: 9Last Post: 07312008, 02:51 PM 
how to right a program that find kth number in two sorted array?
By fireball2008 in forum New To JavaReplies: 8Last Post: 04222008, 04:21 AM 
Recursive Method ==> find minimum value from array
By NatNat in forum New To JavaReplies: 1Last Post: 02162008, 10:10 PM 
Recursive Method ==> find how many times a value is repeated in an array
By NatNat in forum New To JavaReplies: 2Last Post: 02162008, 09:52 PM
Bookmarks