-4

I am writing a code to find duplicate values which have been entered in an array. But i am not able to get code right. Can anyone suggest me with an improvised code then this (using Arraylist)?

package com.Test1.java; 

import java.util.Arrays;
import java.util.Scanner;

public class Test1 {
    private static Scanner s;
    public static void main(String[] args) {
    int n;
    s = new Scanner(System.in);
    System.out.println("Enter the number of values you want to enter:");
    n=s.nextInt();
    int number[]=new int[n];
    System.out.println("Enter the values:");
        for(int i=1;i<=number.length;i++)
        {
            number[i]=s.nextInt();
            }
    System.out.println(“The Values Entered Are:”);
    Arrays.sort(number);
    for(int j=1;j<=number.length;j++)
    {
    if(number[j]==number[j-1])
    {
        System.out.println("The dupliclate number is"+number[j]);
    }

        }
    }
    }

I am getting this error output:

How many values you want to Enter: 
5
Enter the values:
1
2
2
3
4
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
    at com.Test1.java.Test1.main(Test1.java:17)**
tobias_k
  • 81,265
  • 12
  • 120
  • 179
  • Are you intentionally not using an `ArrayList`? – Kon Oct 27 '14 at 14:59
  • 3
    Array are 0 indexed in Java. So instead of iterating from 1 to array.lenth, you must iterate from 0 to array.length-1 – Julien Oct 27 '14 at 15:01
  • Possible duplicate of http://stackoverflow.com/questions/17967114/how-to-remove-duplicates-from-an-array-in-java – Chiseled Oct 27 '14 at 15:06

3 Answers3

3

Try this approach:

  1. Turn the array into a List.
  2. Turn the List into a Set to get each member once.
  3. Check if each member of the Set is in the List more than once.

Java code:

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class SOPlayground {

    public static void main(String[] args) throws Exception {
        Integer numbers[] = new Integer[]{1, 3, 4, 6, 7, 9, 0, 9, 8, 6, 5, 4, 3, 2, 1};

        List<Integer> a = Arrays.asList(numbers);
        Set<Integer> s = new HashSet(a);
        for (Integer i : s) {
            if (Collections.frequency(a, i) > 1) {
                System.out.println(i + " is a duplicate");
            }
        }
    }
}

Output:

1 is a duplicate
3 is a duplicate
4 is a duplicate
6 is a duplicate
9 is a duplicate
  • Yes, that is possible. Did you try to buil an `Integer[]` using a `Scanner`? –  Oct 28 '14 at 07:41
0

This answer (intentionally) does not provide a full alternative solution using ArrayLists, but instead tries to explain what's wrong with your array-based code, so you can fix it yourself. The problem is here:

for(int i=1;i<=number.length;i++)
{
    number[i]=s.nextInt();
}

In Java (and most other languages) the first index of an array of length n is 0, and the last index is n-1, not 1 and n. Thus, your loop should be

for (int i=0; i < number.length; i++)

And similar for your second for loop.

tobias_k
  • 81,265
  • 12
  • 120
  • 179
-1

Array indexes in JAVA start from 0. Your for loop starts from 1 and goes up to 5 and is incorrect. Hence you're getting ArrayIndexOutOfBounds exception when you try to access number[5]

for(int i=1;i<=number.length;i++)

It should start from 0.

for(int i = 0; i < number.length; i++)

harun
  • 1,889
  • 18
  • 19
  • This is a reasonable answer to explain why the OP is getting ArrayIndexOutOfBounds. why is the downvote? – harun Oct 27 '14 at 15:06