How to Find the Duplicated Number in an Array in Java

How to Find the Duplicated Number in an Array in Java thumbnail
Java uses arrays to store sequences of number values as well as other data types.

If you have a numerical array in a Java program and it contains an unwanted duplicate value, you may need to find it. Using loop structures, your code can iterate through your array, checking each value to see whether it has already occurred and letting you know where the duplicate is. By embedding one loop inside another, your code can check each item against previous items. Give yourself a clear grasp of the process by thinking all the way through what will happen when your code executes each time you add a new control structure.

Instructions

    • 1

      Create your numerical array. If you do not already have a number array in your program, you can use the following sample:

      int[] myNums = {3, 5, 1, 6, 5, 8, 7};

      This line declares and instantiates an array with primitive type integer values. As you can see, the duplicate value is the number five. Prepare two variables for storing the duplicated number value and the position it sits at within the array:

      int dupNum = -1;
      int dupPos = -1;

      By initializing these to negative one, you will be able to tell whether your process finds a duplicate value in the array or not.

    • 2

      Create a loop to iterate through your array. Add the following loop outline structure to your program:

      for(int i=1; i<myNums.length; i++) {
      //process contents here
      }

      This loop will iterate once for every element in your numerical array. Inside the loop, you can implement the checking process, comparing each item to the previous items in the structure. Store the current number value in a local variable inside your loop:
      int currNum = myNums[i];

      This value represents the integer at the current position whenever the loop iterates.

    • 3

      Create a second loop inside the first one. Add the following loop outline after you store the current value in its variable:

      for(int j=0; j<i; j++) {
      //check previous values
      }

      Inside this loop, you can compare the current value to those appearing at earlier positions in the array. This way you can tell if the current value is a duplicate.

    • 4

      Compare the current number to previous values. Inside your second for loop, add the following conditional statement:

      if(currNum==myNums[j]) {
      //the value is a duplicate
      }

      If this test returns a true value, it means that the current array element is equal to a previous one, with its position indicated by the second loop counter. Inside the conditional if statement, instruct the program what to do when it encounters a duplicate:

      dupNum = currNum;
      dupPos = i;
      break;

      The code sets the values of the duplicated number and its position, so that they will be accessible when the loop finishes. No further point exists for continuing with the loop at this stage, so the break statement stops it from iterating further.

    • 5

      Break out of the first loop. The break statement only breaks your code out from the nearest loop. This means that the outer loop will continue even when you have found the duplicate value. After the closing bracket for the inside loop, add the following conditional statement:

      if(dupNum>=0) break;

      If the duplicate has not been found, the outer loop will continue executing. You can add the following test statement after your outer loop closes:

      System.out.println("Duplicated number: " + dupNum + ", position: " + dupPos);

      If your code has not located a duplicate, both variables will still be storing values of negative one.

Tips & Warnings

  • Embedded loops can be challenging at first due to the complex flow of execution involved, but they can help you to implement processes concisely.

  • If you need to detect more than one duplicate, you could need to create a second array to store these values.

Related Searches:

References

Resources

  • Photo Credit Jupiterimages/Photos.com/Getty Images

Comments

Related Ads

Featured