Home > Net >  write a program in java that accept n number of elements in an array then display only duplicate ele
write a program in java that accept n number of elements in an array then display only duplicate ele

Time:08-06

write a program in java that accept n number of elements in an array then display only duplicate elements for example if we enter 12352342678898 it will show 238 i have tryed this but this is very long

import java.util.*; class JavaApplication1 {

public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    System.out.print("Enter Size: ");
    int s=sc.nextInt();
    int a[]=new int[s];
    int t[]=new int[s];
    int p=0;
    System.out.println("Enter numbers :-");
    for(int i=0;i<s;i  )
    {
        a[i]=sc.nextInt();
    }
    for(int i=0;i<s;i  )
    {
        boolean flag=false;
        for(int j=i 1;j<s;j  )
        {
            if(a[i]==a[j])
            {
                flag=true;
                break;
            }
        }
        if(flag==true)
        {
            t[p]=a[i];p  ;
        }
    }
    System.out.print("duplicate elements are:- ");
    for(int i=0;i<p;i  )
    {
        boolean flag=false;
        for(int j=i 1;j<s;j  )
        {
            if(t[i]==t[j])
            {
                flag=true;
                break;
            }
        }
        if(flag==false)
        {
            System.out.print(t[i] " ");
        }
    }
}

}

CodePudding user response:

I'll tell you the keypoints of the solution I'd implement.

  • Do not force the user to establish the size of the array beforehand: just say press "q" to exit.
  • The idea of using the index of the array as the numbers is not a bad one as long as you are the only programmer. If not I'd use a dictionnary with "element":"number of repetitions". You can then add letters to your program
  • Finally, you need to make it more modular. First create a function that takes all the numbers and return an array. Then create a function that filters that array and returns the repeated elements. Finally, create a function that writes that array. In your main you should only call those 3 functions.

CodePudding user response:

You can create a Set and as you go through the numbers the first time, you can add them to the set. It will not allow you to add duplicate values because sets do not allow duplicates. This will result in you only having to loop through the numbers once and then at the end, display the numbers contained in the set. I hope that helps

CodePudding user response:

One possibility is to sort the array a, e.g., using java.util.Arrays.sort(a);. Then just iterate through a and check for repeating numbers. That part could look like this:

    Arrays.sort(a);
    Integer prev = null;
    Integer printed = null;
    for (int val : a) {
            if (prev != null && val == prev && (printed == null || val != printed)) {
                    System.out.print(val   " ");
                    printed = val;
            }
            prev = val;
    }

This is quite an ugly program and solution (my code). While this works from algorithmic point of view, I do not recommend this as a good programming practice. For instance, using null checks... It is also advisable to move the detection of duplicate numbers into separate method (or better, class), without printing out. This is mixing computation with user interface, bad practice.

  • Related