1

I've checked everything but still duplicate the last array for some reason. What seems to be the problem? In the Program, the first thing is to ask a user to input array size, then input numbers to that size. Next, the code shall remove the duplicate number that a user inputted. Lastly, the output will display the elements of the array without any duplicate.

Below is the program/code for the same:-

    import java.util.Scanner;

     public class Finals
     {
      private static Scanner sc;

      public static void main(String[] args)
      {

        int tao, hayop, counter, bilang = 1, result, taonghayop;

        sc = new Scanner(System.in);

        System.out.print("Enter array size: ");

        int userInput = sc.nextInt();
        int userInput1 = userInput;
        int[] userDatabase = new int[userInput];

        for(counter=0;counter<userInput;++counter)
        {
            System.out.print("Enter array elements of index " +bilang +": ");
            userDatabase[counter] = sc.nextInt();
            bilang++;
        }

        for(tao=0;tao<userInput;++tao)
        {
            for(hayop=tao+1;hayop<userInput;)
            {

                if(userDatabase[tao] == userDatabase[hayop])
                {
                    for(taonghayop = hayop; taonghayop<userInput;taonghayop++)
                    {
                        userDatabase[taonghayop] = userDatabase[taonghayop+1];
                    }
                    userInput = userInput-1;
                }       
                else
                    hayop++;        
            }
        }

        System.out.print("The number(s) are: " + userDatabase[counter]);

        for (result=0; result<=userInput1; result++) 
        {
            if (result<userInput1) 
            {
                System.out.print(" ");
                System.out.print(userDatabase[result]);
                System.out.print(",");

            }
            else if(result==userInput1)
            {
            System.out.print(" and ");
                System.out.print(userDatabase[result]);
                System.out.print(".");
                break;
            }
        }
    }
}
Abhinav
  • 530
  • 8
  • 21
Ozaki TM
  • 19
  • 5
  • At least one problem is that in the innermost for-loop you increment `taonghayop` twice (in the for-loop increment expression and later in the code) – Michael Butscher Nov 11 '18 at 02:56
  • remove `tanghayop` increment. Make it increment once. Now I get error with the line `userDatabase[hayop] = userDatabase[taonghayop];`. I tried using `userDatabase[taonghayop+1];`, I still get errors. – Ozaki TM Nov 11 '18 at 03:04
  • What is the error or output (edit the question to show it as properly formatted text)? – Michael Butscher Nov 11 '18 at 03:07
  • Edited the question. CMD stuck loading when it has duplicate number withing array 1 and 2. Somehow, when the duplicated number is in array 3 up, the the duplicated number is eliminated. – Ozaki TM Nov 11 '18 at 03:16
  • In the innermost loop you assign to `userDatabase[hayop]` but `hayop` doesn't change so all following numbers are assigned to the same index in the array (previous value is overwritten). – Michael Butscher Nov 11 '18 at 03:46
  • Edited the question. okay. I replace `userDatabase[hayop]` to `userDatabase[taonghayop]` as seen in the code above. I still get error. Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException – Ozaki TM Nov 11 '18 at 04:02
  • In the inner loop `taonghayop` can become `userInput - 1` so `userDatabase[taonghayop + 1]` becomes `userDatabase[userInput]` but `userInput` is too large as array index (at least before it is decremented first time). – Michael Butscher Nov 11 '18 at 04:19
  • What should I do? Im stuck to that line. Thanks for helping out! – Ozaki TM Nov 11 '18 at 04:37
  • @Ozaki TM Why've you made your program so costly using these loops & all. Why don't you use _Collections_ API viz. **_LinkedHashSet_** to remove duplicates? Aren't you making your program too verbose with the boilerplate code? – Abhinav Nov 11 '18 at 05:18
  • @Abhinav, I don't know Collections API viz. Just starting to learn java with cmd and sublime. only knew few codes. – Ozaki TM Nov 11 '18 at 05:40
  • @Ozaki TM All right! No issues. Since you don't know about _Collections_ API in Java then you can check this: [https://stackoverflow.com/questions/17967114/how-to-efficiently-remove-duplicates-from-an-array-without-using-set] which doesn't use _**Set**_ API in Java. This link will definitely help you out! – Abhinav Nov 11 '18 at 05:45

0 Answers0