Java program to find the duplicate elements in an array of Strings

Java program to find duplicate elements in an array :

In this tutorial, we will learn how to find duplicate elements in an array of strings. We will learn this using two different approaches :

  1. Using Brute force method.

  2. Using HashSet

In Brute force method, the program will scan each elements one by one. For each element, we will compare it with all other elements of the array. The complexity of this method is O(n^2).

Using one HashSet, we can reduce the complexity to O(n) . Since HashSet holds no duplicate elements, we will try to add all elements of the array to a HashSet. If any addition failed, means that element is already added, we will print that element as duplicate.

Let’s take a look into the program first :

Java program to find duplicate elements in an array :

import java.util.HashSet;
import java.util.Scanner;

public class Main {

    //8
    private static void findDuplicateUsingBruteForce(String[] array) {
        //9
        System.out.println("\nDuplicate elements using Brute Force method : \n");

        //10
        boolean duplicateElementsFound = false;

        //11
        for (int i = 0; i < array.length - 1; i++) {
            for (int j = i + 1; j < array.length; j++) {
                //12
                if (array[i].equals(array[j])) {
                    duplicateElementsFound = true;
                    System.out.print(array[i] + " ");
                }
            }
        }

        //13
        if (!duplicateElementsFound) {
            System.out.println("No duplicate elements found.");
        }
    }

    //15
    private static void findDuplicateUsingHashSet(String[] array) {
        //16
        System.out.println("\nDuplicate elements using HashSet : \n");

        //17
        HashSet stringSet = new HashSet<>();

        //18
        boolean duplicateElementsFound = false;

        //19
        for (String element : array) {
            //20
            if (!stringSet.add(element)) {
                duplicateElementsFound = true;
                System.out.println(element + " ");
            }
        }

        //21
        if (!duplicateElementsFound) {
            System.out.println("No duplicate elements found.");
        }
    }

    public static void main(String[] args) {
        //1
        String[] wordsArray;
        Scanner scanner = new Scanner(System.in);

        //2
        int count;

        //3
        System.out.println("Enter number of elements : ");
        count = scanner.nextInt();

        //4
        wordsArray = new String[count];

        //5
        for (int i = 0; i < count; i++) {
            System.out.println("Enter string " + (i + 1) + " : ");
            wordsArray[i] = scanner.next();
        }

        //6
        System.out.println("You have entered : ");
        for (int i = 0; i < wordsArray.length; i++) {
            System.out.print(wordsArray[i] + " ");
        }

        //7
        findDuplicateUsingBruteForce(wordsArray);

        //14
        findDuplicateUsingHashSet(wordsArray);
    }


}

Explanation :

The commented numbers in the above program denotes the step number below :

  1. Declare one String array variable to store all user input strings. Create one Scanner object to read user input.

  2. Create one int variable to get the total count of the array.

  3. Ask the user to enter the total number of elements . Store it in count variable.

  4. Create the previously declared array variable. This array is equal to count.

  5. Using a for loop, read all elements user enter and store it in the String array.

  6. Print all user entered values.

  7. Call method findDuplicateUsingBruteForce() to find all duplicate elements in the array using Brute force.

  8. This method takes one String array as input . We are passing the String array wordsArray to this method.

  9. Find all duplicate elements.

  10. Create one boolean duplicateElementsFound and assign it false. This flag will be true if any duplicate element is found.

  11. Run two for loops. The outer loop will scan all elements one by one and the inner loop will compare each element with all other elements.

  12. If any duplicate element found , assign flag duplicateElementsFound true and print out the element.

  13. If duplicateElementsFound is false, means no duplicate element found. Print the message.

  14. Call the method findDuplicateUsingHashSet to find duplicate elements using a Hash Set.

  15. findDuplicateUsingHashSet takes one array of String elements.

  16. Print out all duplicate elements using Hash Set.

  17. Create one HashSet of String stringSet.

  18. Create boolean flag duplicateElementsFound and assign it value false. true means duplicate elements found and false means no duplicate elements found.

  19. Using one for loop , scan all elements one by one.

  20. Check if an element can be add to the HashSet or not. It no, means the element is already added and this is a duplicate. Print out the element . Also, assign duplicateElementsFound value true.

  21. If no duplicate element is found, print the message.

Sample Output :

Enter number of elements :
5
Enter string 1 :
11
Enter string 2 :
22
Enter string 3 :
33
Enter string 4 :
44
Enter string 5 :
11
You have entered :
11 22 33 44 11
Duplicate elements using Brute Force method :

11
Duplicate elements using HashSet :

11


Enter number of elements :
4
Enter string 1 :
aa
Enter string 2 :
bb
Enter string 3 :
cc
Enter string 4 :
dd
You have entered :
aa bb cc dd
Duplicate elements using Brute Force method :

No duplicate elements found.

Duplicate elements using HashSet :

No duplicate elements found.

Similar tutorials :