madu
madu

Reputation: 5450

Finding the similarity of two String arrays

I apologize if there is a similar question asked before but only one I could find was How to find similar patterns in lists/arrays of strings

The problem is simple. There are two large arrays of strings. And I need to find out the similarity. Similarity is simply the number of similar strings.

String [] A = {"A", "B", "C", "D"}; String [] B = {"X", "Y", "B", "D"};

Similarity between these two lists is 2.

The obvious solution is to the brute force way. Just compare each word with every other word. But what is the next best solution?

Is using a hash map an effective solution? Go through one list and put every word in to a hash map. Then see how many of the second list keys are contained in the hash map.

Is this the generally accepted way of performing this type of problem? Seems there are tree-based solutions but they do not seem straight forward.

Thank you.

Upvotes: 1

Views: 1400

Answers (2)

hoaz
hoaz

Reputation: 10143

You can backup each array into Set and then find intersection:

Set<String> firstSet = new HashSet<String>(Arrays.asList(firstArray));
Set<String> secondSet = new HashSet<String>(Arrays.asList(secondArray));
firstSet.retainAll(secondSet);
System.out.println(firstSet.size());

Upvotes: 2

Brian
Brian

Reputation: 7316

Use a HashSet. It provides functionality for taking intersections.

list1 // your 1st list
list2 // your second list

Set<Type> s1 = new HashSet<Type>();
s1.addAll(list1); // add your values from list 1
Set<Type> s2 = new HashSet<Type>(); 
s2.addAll(list2); // add your values from list 2

Set<Type> intersection = new HashSet<Type>(s1);
intersection.retainAll(s2);
System.out.println(intersection.size());

You can refer to the Set interface in The Java Tutorials

Upvotes: 2

Related Questions