Write a Java program that creates an Array of string values and displays the duplicate values.
public class Main {
public static void main(String[] args) {
String[] array = {"Five","One", "Two", "Three", "One", "Three", "Five", "One"};
for (int i = 0; i < array.length; i++) {
boolean shown = false;
for (int j = i - 1; j >= 0; j--) {
if (array[j].equals(array[i])) {
shown = true;
break;
}
}
for (int j = i + 1; j < array.length && !shown; j++) {
if (array[j].equals(array[i])) {
System.out.println(array[i]);
break;
}
}
}
}
}
Comments
Leave a comment