0

I have an array of numbers in Java and need to output the ones that consist of only duplicated digits. However, my code throws an ArrayIndexOutOfBoundsException. Where is the problem?

int[] inputValues= {122, 2, 22, 11, 234, 333, 000, 5555, 8, 9, 99};

for (int i = 0; i < inputValues.length; i++) {
    int numberLength = Integer.toString(inputValues[i]).length();
//  System.out.println(numberLength);
    if (numberLength > 1) { //more than one digit in the number
        String s1 = Integer.toString(inputValues[i]);
        String[] numberDigits = s1.split("");

        for (int j = 1, k = 1; j < numberDigits.length; k++) {
            if (numberDigits[j].equals(numberDigits[k + 1])) {
                System.out.println("Duplicate values are:");
                //I need to print 22,11,333,000,5555,99 
            }
        }
    }
}
6
  • haven't all numbers x > 9 || x < -9 more than one digit? There's no need to convert it into a String. Commented Nov 21, 2014 at 9:40
  • @dusky,to get desired output could you please edit my code. Commented Nov 21, 2014 at 9:42
  • @GangadharB see my answer below , if that suits Commented Nov 21, 2014 at 10:20
  • @ShubhangMalviya,please check Sujit Thombare answer,very simple one.but there is one error in his code.if array value are 10,20,30,333,999,then giving wrong output. Commented Nov 21, 2014 at 10:43
  • @GangadharB First for my answer you need to call getDuplicate(int[] arr) method and that all. It will return the correct answer. If you didn't understand my code , then plz comment. Commented Nov 21, 2014 at 10:53

6 Answers 6

3

There is no condition to stop the inner loop when k gets too big. j never changes in the inner loop, so j < numberDigits.length will either always be true or always be false.

Sign up to request clarification or add additional context in comments.

1 Comment

David,to get desired output could you please help me.
1
public static void  main(String[] args) {


    int[] inputValues={122,2,22,11,234,333,000,5555,8,9,99,1000};



    System.out.println("Duplicate values are:");
      for (int i = 0; i < inputValues.length; i++) {

          String strNumber = new Integer(inputValues[i]).toString();// get string from array

          if(strNumber.length()>1)  // string length must be greater than 1
          {

              Character firstchar =strNumber.charAt(0);  //get first char of string

              String strchker =strNumber.replaceAll(firstchar.toString(), ""); //repalce it with black



              if(strchker.length()==0)   // for duplicate values length must be 0
              {
                  System.out.println(strNumber);

              }

          }


       }


      /*
       * output will be 
       * Duplicate values are:
        22
        11
        333
        5555
        99
       * 
       * 
       */
}

This is what you want.....

7 Comments

,if array having 10,20,30 then resulting is not correct.
Please give me your input array and tell me what output you want..... What exactly you want ?
,input is int[] inputValues = {122,10,20,101,2, 22, 11, 234, 333, 5555, 8, 9, 99}; output is:22,11,333,5555,99
,code is working fine.for numbere 0000 output has to come in Duplicate value.It's not printing.Is it valid one or not.
if you working with int array then 0000 is consider as only 0 ....so it will not be coming in output.... if you want 0000 in output then go with String array.
|
1

This line is the culprit here -

for (int j = 1, k = 1; j < numberDigits.length; k++) {
    if (numberDigits[j].equals(numberDigits[k + 1])) {
        System.out.println("Duplicate values are:");//i need to print 22,11,333,000,5555,99,etc.
    }
}

The loop has a condition that's always true as value of j is always 1. Since k keeps on increasing by 1 for each iteration ( which are infinite btw ), the index goes out of array bounds.

Try -

for (int j = 0, k = 1; k < numberDigits.length; k++) {
    boolean isDuplicate = true;
    if (!numberDigits[j].equals(numberDigits[k])) {
        isDuplicate = false;
        break;
    }
}
if( isDuplicate ) {
    System.out.println("Duplicate values are:"+inputValues[i]);
}

Comments

1

Sorry for joining the party late. I think following is the piece of code you’re are looking for

private int[] getDuplicate(int[] arr) {
        ArrayList<Integer> duplicate = new ArrayList<Integer>();

        for (int item : arr) {
            if(item > 9 && areDigitsSame(item)) {
                duplicate.add(item);
            }
        }

        int duplicateDigits[] = new int[duplicate.size()];

        int index = 0;
        for (Integer integer : duplicate) {
            duplicateDigits[index ++] = integer;
        }
        return duplicateDigits;
    }

    public boolean areDigitsSame(int item) {
        int num = item;
        int previousDigit = item % 10;
        while (num != 0) {
            int digit = num % 10;
            if (previousDigit != digit) {
                return false;
            }
            num /= 10;
        }

        return true;
    }

Now , use it as below

int[]inputValues={122,2,22,11,234,333,000,5555,8,9,99};
int[] duplicates = getDuplicate(inputValues);

That's all

Enjoy!

9 Comments

I've added the usage , in case
Shubhang Malviya,Thank you for your help.
@GangadharB You're welcome :) but If you really like it , would you mind upvoting my answer.
Shubhang Malviya,i feel comfort with Sujit Thombare code (easy to understand).Sorry Shubhang Malviaya.
@GangadharB upvoting an answer and accepting an answer are two different things :) , upvoting means that either you gain knowledge from the answer or that can be an alternative answer , but accepting answer means that you want to go ahead with that answer , so plz don't mis understand my suggestion :)
|
0
    public static void  main(String[] args) {


        String[] inputValues={"122","2","22","11","234","333","000","5555","8","9","99"};



        System.out.println("Duplicate values are:");
          for (int i = 0; i < inputValues.length; i++) {

              String strNumber = inputValues[i];// get string from array
              if(strNumber.length()>1)  // string length must be greater than 1
              {
                  Character firstchar =strNumber.charAt(0);  //get first char of string

                  String strchker =strNumber.replaceAll(firstchar.toString(), "0"); //repalce it with 0

                if(Integer.parseInt(strchker)==0)   //if all values are duplictae than result string must be 0
                {
                  System.out.println(strNumber);
                }

              }


           }

    }
   // /// result will be
 /*  Duplicate values are:
   22 
   11
   333
   000
   5555
   99
*/

if you want int array then you will not able to get "000" as duplicate value.

5 Comments

Sujit Thombare ,thank you,output is ok ignore 000 i need int array values.could you please help me.
then replace 5th line with ` String strNumber = new Integer(inputValues[i]).toString();` and replace your String array with int array
Sujit Thombare ,if array having 10,20,30 values then output is wrong.please edit your code.
Please give me your input array and tell me what output you want..... What exactly you want ?
,input: int[] inputValues = {122,10,20,101,2, 22, 11, 234, 333, 5555, 8, 9, 99}; output is:22,11,333,5555,99
0

@ line 13: the s1.split("") results to [, 1, 2, 2] for 122 . Hence your numberDigits.length is 4. The loop runs from j = 1 to 3 ( j < numberDigits.length); hence the numberDigits[k + 1 ] is evaluated for index 4 which is unavailable for [, 1, 2, 2].

Another point is worth noting is int[]inputValues will always store 000 as 0 only.

The below mentioned method will take a integer number and will return true and false based on your requirement. It will use xor operator to check repetitive digits.

private static boolean exorEveryCharacter(int currentValue) {
        int result = 0;
        int previousNumber = -1;

        while (currentValue != 0) {
            int currentNumber = currentValue % 10;

            if(previousNumber == -1){
                previousNumber = currentNumber;
            }
            else{
                result = previousNumber ^ currentNumber;
            }

            currentValue /= 10;
        }
        return result == 0;
    }

1 Comment

@GangadharB: The above code uses a simple property that for any two same values there xor will return 0. So you just need to xor the individual elements of any number. If the final output is 0 then it had same digits. I think its very simple. Feel free to ask any doubt.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.