w3resource

Java: Find the duplicate values of an array of string values


13. Find duplicates in string array

Write a Java program to find duplicate values in an array of string values.

Pictorial Presentation:

Java Array Exercises: Find the duplicate values of an array of string values


Sample Solution:

Java Code:

// Define a class named Exercise13.
public class Exercise13 {
    // The main method where the program execution starts.
    public static void main(String[] args) {
        // Declare and initialize a string array 'my_array'.
        String[] my_array = {"bcd", "abd", "jude", "bcd", "oiu", "gzw", "oiu"};
 
        // Iterate through the elements of the string array.
        for (int i = 0; i < my_array.length-1; i++) {
            for (int j = i+1; j < my_array.length; j++) {
                // Check if two string elements are equal and not the same element.
                if ((my_array[i].equals(my_array[j])) && (i != j)) {
                    // If a duplicate is found, print the duplicate element.
                    System.out.println("Duplicate Element is : " + my_array[j]);
                }
            }
        }
    }    
}

Sample Output:

Duplicate Element is : bcd                                                                                    
Duplicate Element is : oiu

Flowchart:

Flowchart: Java exercises: Find the duplicate values of an array of string values


For more Practice: Solve these Related Problems:

  • Write a Java program to find duplicate words in a sentence.
  • Write a Java program to count how many times each string appears in an array.
  • Write a Java program to remove duplicate strings while maintaining the original order.
  • Write a Java program to find and print duplicate strings that appear more than twice in an array.

Go to:


PREV : Find duplicates in integer array.
NEXT : Common elements in two string arrays.

Java Code Editor:

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.