Results 1 to 8 of 8
  1. #1
    mar1 is offline Member
    Join Date
    Jan 2013
    Posts
    1
    Rep Power
    0

    Default 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 06:24 AM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: Counting duplicates in an array

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    sidda is offline Member
    Join Date
    Apr 2014
    Posts
    9
    Rep Power
    0

    Default Re: Counting duplicates in an array

    Please try this !!

    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. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    926
    Rep Power
    2

    Default 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.

    No spoonfeeding please.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  5. #5
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    926
    Rep Power
    2

    Default 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.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,375
    Blog Entries
    7
    Rep Power
    20

    Default Re: Counting duplicates in an array

    Quote Originally Posted by SurfMan View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,758
    Rep Power
    5

    Default 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.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    sidda is offline Member
    Join Date
    Apr 2014
    Posts
    9
    Rep Power
    0

    Default 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++)
            {
                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)
                           
            		   );
            
           }
             
            
     
        }
     
    }

Similar Threads

  1. Removing Duplicates in an Array
    By jerseyjava in forum New To Java
    Replies: 7
    Last Post: 02-01-2012, 07:46 PM
  2. Remove Duplicates in Array
    By HSKrustofsky in forum New To Java
    Replies: 11
    Last Post: 09-06-2011, 05:47 AM
  3. Getting rid of duplicates in my Array problem
    By 'Rasta. in forum New To Java
    Replies: 6
    Last Post: 11-20-2010, 05:51 PM
  4. Duplicates in String Array
    By turnergirl24 in forum New To Java
    Replies: 1
    Last Post: 11-04-2009, 11:09 PM
  5. FInd the no. of duplicates in an array
    By singularity in forum Advanced Java
    Replies: 3
    Last Post: 09-04-2009, 09:25 AM

Posting Permissions

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