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

    Default Print Duplicates element of Array

    As you can see my below code, I'm newbie to programming language & sure this code is not best way to find the duplicate elements in a given array.
    Could you please suggest any alternative method for an optimized code.



    Java Code:
    import java.util.Arrays;
    public class Find_Dupliicate_ArrayElement {
    
    	public static void main(String[] args) {
    
    		int[] Array1 = {1, 9,8,1,2,8,9,7,10, -1, 1, 2, 3, 10, 8, -1};
    		// Store the array length
    		int size = Array1.length;
    	//Sort the array
    		Arrays.sort(Array1);
    //Set a flag with staus as False 	
    		boolean status = false;
    //loop thru array elements 
    		for(int i = 0; i<= size-1; i++){
    			
    			while (Array1[i] == Array1[i+1]){
    				// Set status when two array elements are equal
    				status = true;
    				
    				if(status){
    //When status is set, Increment index of array
    					i++;
    
    					if(i!= size-1){
    //If Arrayindex is not the end of the array and sucessive elements are not equal, Print the first element of comparision
    					if(Array1[i]!= Array1[i+1]){
    						System.out.println("Found the Duplicate Element in the list " + Array1[i]);
    						status = false;
    						break; 
    					}
    //If Arrayindex is @  end of the array and previous condition is not valid Print the first element of comparision
    					}
    					else{
    						System.out.println("Found the Duplicate Element in the list " + Array1[i]);
    						status = true;
    						break; 
    					}
    				}
    			}
    		
    			
    		
    		}
    
    	}
    
    }

    O/p :

    Found the Duplicate Element in the list -1
    Found the Duplicate Element in the list 1
    Found the Duplicate Element in the list 2
    Found the Duplicate Element in the list 8
    Found the Duplicate Element in the list 9
    Found the Duplicate Element in the list 10

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Print Duplicates element of Array

    These two lines of code do not make sense:
    Java Code:
                    status = true;
                     
                    if(status){
    Why have the if? status is always true just after it is assigned the value true.

    Can you describe what the algorithm/design the code is trying to use to do the search?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,411
    Rep Power
    5

    Default Re: Print Duplicates element of Array

    The best way is the one which works for you and that you understand. Your general approach aside from any algorithmic errors is rather classic. I for one would simply use a hash table to mark the first encounter of a value. No sort required but it does incur the overhead of a hash table. One advantage is that it easily allows you to denote at what index in the original list the value was duplicated.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Counting duplicates in an array
    By mar1 in forum New To Java
    Replies: 7
    Last Post: 04-16-2014, 04:06 PM
  2. Removing Duplicates in an Array
    By jerseyjava in forum New To Java
    Replies: 7
    Last Post: 02-01-2012, 07:46 PM
  3. Remove Duplicates in Array
    By HSKrustofsky in forum New To Java
    Replies: 11
    Last Post: 09-06-2011, 05:47 AM
  4. Remove duplicates in 2D array
    By lakshmibvaraprasad in forum New To Java
    Replies: 5
    Last Post: 07-22-2011, 08:49 PM
  5. Duplicates in String Array
    By turnergirl24 in forum New To Java
    Replies: 1
    Last Post: 11-04-2009, 11:09 PM

Posting Permissions

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