I have made a pair word detector, which gives me an output of true or false.
If the value in the string array contains the same string letter (duplication) will return true.
I used below code using nested loops
Now, I want to do the same concept without the usage of any loops?
How can I do this, any examples or what type of java collection framework is needed?
Thank you
Main:
public class Main
{
public static void main(String[] args)
{
String[] box = {"Monkey","Lion","Elephant","Zebra","Tiger", "Hippo"};
String[] box2 = {"Shop","Biscuit","Cake","Cake"};
String[] box3 = {"Entrance","Gate","Price","Door","Gate"};
String[] box4 = {"Female","Male"};
System.out.println(Pairfinder.test(box)); //false
System.out.println(Pairfinder.test(box2)); //true
System.out.println(Pairfinder.test(box3)); //true
System.out.println(Pairfinder.test(box4)); //false
}
}
Sub:
public class Pairfinder
{
public static boolean test(String[] value)
{
for (int i = 0; i < value.length; i++) {
for (int j = 0; j < value.length; j++) {
if (value[i].equals(value[j]) && i != j) {
return true;
}
}
}
return false;
}
}