Java: Find the duplicate values of an array of string values
Java Array: Exercise-13 with Solution
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.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/java-exercises/array/java-array-exercise-13.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics