-2

In Java, I have two ArrayLists:

A = [Ab, cd, df, FE, ...]
B = [ab, cde, de, fE, ...]

If the lists are a little big, the brute force method is very slow:

for(String a : A) {
  for(String b : B) {
     if(a.equalsIgnoreCase(b)) {
        System.out.println("duplicate: " + a "->" + b);
     }
  }
}

What's the best way to make it faster, but not super complicated to implement?

user697911
  • 10,043
  • 25
  • 95
  • 169

1 Answers1

2

simplest implementation is to use sets, like following:

final String A[] = {"Ab", "cd", "df", "FE"};
final String B[] = {"ab", "cde", "de", "fE"};

final Set<String> set = new HashSet<>(A.length);
for (final String a : A)
    set.add(a.toLowerCase());
for (final String b : B)
    if (set.contains(b.toLowerCase()))
        System.out.println("duplicate: " + b);
Iłya Bursov
  • 23,342
  • 4
  • 33
  • 57