Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Algorithm to compare one array of strings to many array of strings

I am a bit stuck on this one problem in the current project I am working on:

I want to take one ArrayList<String> (call it A) and compare it to many ArrayList<String>, making note of how many strings match in these array comparisons. Then, I want to order the multiple string arrays in order from most similar to least similar to A.

Does anyone know any fast algorithms to do this? Not looking for code so much as algorithms, but I am working in Java.

Thanks!

like image 746
CatLord Avatar asked Oct 01 '22 06:10

CatLord


1 Answers

My suggestion :

  1. First put all the Strings of the reference ArrayList in a Set.
  2. Go over all the members of each of the other ArrayLists, and using set.contains(string) find out how many matches each array list contains.
  3. For each ArrayList, create an object that wraps an ArrayList in addition to the number of matches for that ArrayList.
  4. Finally, sort those wrapper objects according to the number of matches.
like image 162
Eran Avatar answered Oct 04 '22 19:10

Eran