Java: Find the duplicate values of an array of string values
Write a Java program to find duplicate values in an array of string values.
Pictorial Presentation:
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:
Java Code Editor:
Previous: Write a Java program to find the duplicate values of an array of integer values.
Next: Write a Java program to find the common elements between two arrays (string values).
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics