Results 1 to 2 of 2
  1. #1
    maz09 is offline Member
    Join Date
    Jan 2010
    Posts
    13
    Rep Power
    0

    Default How to detect duplicate values in an Array?

    Hi,

    I need to find duplicate values in an array and get their indexes, by comparing every value with another.

    my code is below:

    Java Code:
    if array cotains: 0 1 1 0 1
    
    int[] array = new int[5];
    
    
    for(int i = 0; i<array.length(); i++{
    
      for(int j=1; j<array.length(); j++{
         if(array[i]==array[j]{
              //store index i and j
         }
    }
    the way i have searched the array doesn't compare every possibility. please help

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    1. You need a closing paren on the if and for statements.
    2. length is a "property" not a method
    3. The outer loop need only go to array.length - 1 (there is no reason to compare the last index to any other index when all the other indexes have already been compared to it)
    4. Make the inner loop start a i + 1 rather than 1 (i.e. there is no reason to compare index 5 (outer) to index 1 (inner) when you've already compared index 1 (outer) to index 5 (inner)).

Similar Threads

  1. Replies: 1
    Last Post: 03-08-2010, 02:34 PM
  2. Error if array contains duplicate integers
    By lithium002 in forum New To Java
    Replies: 4
    Last Post: 12-05-2009, 09:58 AM
  3. Detect "on duplicate key" - Mysql
    By Jazzperson in forum Java Servlet
    Replies: 6
    Last Post: 05-31-2009, 02:17 PM
  4. Counting Duplicate Variables in an Array
    By Npcomplete in forum New To Java
    Replies: 2
    Last Post: 10-24-2008, 08:33 PM
  5. How to make a hashmap to allow duplicate values?
    By Preethi in forum New To Java
    Replies: 0
    Last Post: 02-08-2008, 01:35 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
  •