# Counting duplicates in an array

• 01-23-2013, 01:22 AM
mar1
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:
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!
• 01-23-2013, 05:55 AM
DarrylBurke
Re: Counting duplicates in an array
• 04-16-2014, 10:47 AM
sidda
Re: Counting duplicates in an array

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;                                         }                                 }                         }                                                                         }         } }```
• 04-16-2014, 10:50 AM
SurfMan
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.

• 04-16-2014, 10:52 AM
SurfMan
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.
• 04-16-2014, 12:38 PM
JosAH
Re: Counting duplicates in an array
Quote:

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
• 04-16-2014, 12:44 PM
gimbal2
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.
• 04-16-2014, 04:06 PM
sidda
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,

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++)         {             arr.add(Array1[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]) )                     {               set.add(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)                                                 );               }                       }   }```