Results 1 to 2 of 2
  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 Moderator
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    9,928
    Rep Power
    16

    Default Re: Counting duplicates in an array

    Why do they call it rush hour when nothing moves? - Robin Williams

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
  •