# Thread: Counting duplicates in an array

1. Member
Join Date
Jan 2013
Posts
1
Rep Power
0

## Counting duplicates in an array

It's supposed to count all of the duplicates in an array and print out how many occurrences of the value starting at whatever index, or if there are no duplicates state that. Basically:
No duplicates with value 1 beyond Index 0
There are 3 more occurrences of value 2 starting at index 1
There are 2 more occurrences of value 2 starting at index 2....

This is what I've got so far:
Java Code:
```public static void main(String[] args) {
int[] arr = {1, 2, 2, 3, 4, 2, 4, 3, 0, 5, 3, 2};

for(int i = 0; i<arr.length; i++){
int count = 0;
for(int j = i+1; j<arr.length; j++){
if((arr[j] == arr[i]) && (i!=j)){
count++;
System.out.print("There are " + count + " more occurrences of ");
System.out.println(arr[i] + " starting at index " + i);
}

}

}

}```
If anything is correct I will be very surprised, haha.
I've been through many threads on tons of different websites about this and none of it is helping. I'm really new to this (obviously) and I am really, really, really stuck, so help would be greatly appreciated. Thank you so much!
Last edited by mar1; 01-23-2013 at 07:24 AM.

3. Member
Join Date
Apr 2014
Posts
9
Rep Power
0

## Re: Counting duplicates in an array

Java Code:
```import java.util.Arrays;

public class CountingDuplicates {

public static void main(String[] args) {
int[] Array1 = {1, 9,8,1,2,8,9,7,10, -1, 1, 2, 3, 10, 8, -1, -1,-1};

int size = Array1.length;

Arrays.sort(Array1);
for(int k: Array1){
System.out.print(k+"'");
}
System.out.println("");
boolean status = false;
for(int i = 0; i<= size-1; i++){

int start=i;
int end, lastend, ldiff;

while (Array1[i] == Array1[i+1]){

status = true;
System.out.println("Found the Duplicate Element @ index " +i);

if(status){
i++;

if(i!= size-1){

if(Array1[i]!= Array1[i+1]){
System.out.println("Found the Duplicate Element @ index " +i);
end = i;
int diff = end - start +1;
System.out.println(Array1[i] +" Number of occurence :"+ diff);
status = false;
break;
}
}
else{

System.out.println("Found the last Duplicate Element in the list " + (i+1));
lastend = i;
ldiff = lastend - start+1;
System.out.println(Array1[i] +" Number of occurence :"+ ldiff);
status = true;
break;
}
}
}

}

}

}```

4. ## Re: Counting duplicates in an array

Although your help is appreciated, don't post fully coded solutions. This obviously part of an exercise, so now you ruined it. He gets an A, and he didn't do the work himself. If the OP is smart, he studies your post and tries to understand the difference with his code.

5. ## Re: Counting duplicates in an array

Oh and on top of that, this post is over a year old. If the poster is still working on this, he wil probably fail the CS class.

6. ## Re: Counting duplicates in an array

Originally Posted by SurfMan
Oh and on top of that, this post is over a year old. If the poster is still working on this, he wil probably fail the CS class.
Plus that the 'solution' (mind the quotes) is dead wrong, but I leave it in for others to laugh at.

kind regards,

Jos

7. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
9

## Re: Counting duplicates in an array

You people misunderstand. Its not an answer to the question, Sidda is simply hijacking the thread and asking you to try the code.

8. Member
Join Date
Apr 2014
Posts
9
Rep Power
0

## Re: Counting duplicates in an array

Hey josah,

Im "NOT" claiming this as a Solution (mind the double quotes). I know above code throws "java.lang.ArrayIndexOutOfBoundsException:" however its not giving invalid result.

Below is the fix,

Java Code:
```package arrays;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;

public class counting_Array{

public static void main(String[] args) {

int[] Array1 = {1, 9,8,1,2,8,9,7,10, -1, 1, 2, 3, 10, 8, -1};

HashMap<Integer,Integer> map=new HashMap <Integer,Integer>();

HashSet<Integer> set=new HashSet<Integer>();

ArrayList<Integer> arr=new ArrayList();

for(int t=0;t<Array1.length;t++)
{

if(map.containsKey(Array1[t]))

{
map.put(Array1[t], map.get(Array1[t])+1);
}
else

{
map.put(Array1[t], 1);
}
}

System.out.println(map);

Arrays.sort(Array1);

for (int i=1;i<Array1.length;i++){

if(Array1[i]==Array1[i-1] && !set.contains(Array1[i]) )

{
System.out.println("Duplicate found===> "+Array1[i]);
}
}

System.out.println("*******************************************************");

Iterator it=set.iterator();

while(it.hasNext()){
int ind=(Integer)it.next();
System.out.println(

"first Index of "+ind+"==>"
+arr.indexOf(ind)
+" and last Index is==> "
+ arr.lastIndexOf(ind)
+"and count is==> "+map.get(ind)

);

}

}

}```

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•