Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (7k points)

I am trying to write a code that would find duplicate values and this is my code:

public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc = new Scanner(System.in);
        //System.out.println("Please enter the length of Array: ");
        int[] array = new int[6];
        for(int i =0; i<array.length;i++) {
            System.out.println("Enter value for index "+i+":");
            array[i] = sc.nextInt();
            }

        FindDuplicateInArray obj = new FindDuplicateInArray();

        obj.findDupicateInArray(array);
    }

    public void findDupicateInArray(int[] a) {
        //int pointer = a[0];
        int count=0;
        for(int j=0;j<a.length;j++) {
            for(int k =j+1;k<a.length;k++) {
                if(a[j]==a[k] && j!=k && j<k && count<=1) {
                    count++;
                    if(count==1)
                    System.out.println(a[j]);

                }

            }


        }

    }

By using the above code, I am not getting the expected output, for example:

  1. If I give value 1,2,1,4,3,1 then it is successfully finding the duplicate value 1.

  2. But if I provide 2 set of duplicate values in an array, still it is finding the first duplicate. e.g. 1,2,1,2,1,3. It is giving output only 1.

 I have again written the code making some changes:

public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc = new Scanner(System.in);
        //System.out.println("Please enter the length of Array: ");
        int[] array = new int[6];
        for(int i =0; i<array.length;i++) {
            System.out.println("Enter value for index "+i+":");
            array[i] = sc.nextInt();
            }

        FindDuplicateInArray obj = new FindDuplicateInArray();

        obj.findDupicateInArray(array);
    }

    public void findDupicateInArray(int[] a) {
        //int pointer = a[0];
        int count=0;
        for(int j=0;j<a.length;j++) {
            for(int k =j+1;k<a.length;k++) {
                if(a[j]==a[k] && j!=k && j<k && count<=1) {
                    count++;
                    if(count==1)
                    System.out.println(a[j]);

                }

            }
            **count = 0;**

        }

   

now the new one is giving all duplicate values but duplicate values printing twice. e.g. Input: 1,2,1,2,1,2, Output: 1 2 1 2.

Please suggest how to do this correctly.

1 Answer

0 votes
by (13.1k points)

You can do something like this:

import java.util.Scanner;

public class Main

{

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

        int[] arr = new int[6];

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

            System.out.println("Enter value for index "+i+":");

            arr[i] = sc.nextInt();

            }

        findDupicateInArray(arr);

}

public static void findDupicateInArray(int[] a) {

        int count=0;

        for(int j=0;j<a.length;j++) {

            for(int k =j+1;k<a.length;k++) {

                if(a[j]==a[k]) {

                    count++;

                }

            }

            if(count==1)

               System.out.println(a[j]+"is repeated");

            count = 0;

        }

    }

}

Check this java tutorial to learn more about Java

Related questions

0 votes
1 answer
asked Feb 15, 2021 in Java by Jake (7k points)
0 votes
1 answer
asked Mar 1, 2021 in Java by sheela_singh (9.5k points)
0 votes
1 answer
asked Mar 11, 2021 in Java by Jake (7k points)
0 votes
1 answer
asked Feb 7, 2021 in Java by dante07 (13.1k points)

Browse Categories

...