Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find max match count set from multiple set

I am looking for best solution to find matching set with max string match. Here is example,

inSet = ["a","b","c","x"]

other list of set

set1 = ["a","d","q","s"]
set2 = ["a","m","t","b","z"]
set3 = ["a","x","b","s","r","t"]

in above example, set3 is max. match count (3).

what is best algorithm to find with minimal execution. any pointer or suggestion appreciated.

like image 326
Bharat Avatar asked May 27 '26 18:05

Bharat


1 Answers

Let us have Set<String> set and Guava.Sets:

Set<Set<String>> set = new Set<>();
//add Set<String>s
Set<String> maxMatchSet = set.stream()
                              .max(Comparator.comparingInt((value -> Sets.intersection(value, inSet).size()))
                              .get();

OK, now some theory. ["a", "b"] isn't a set but an array (or a list). We have different data structures in Java. Sets are represented in {}.

Anyway, what matters is the code.

Set<String> set = new HashSet<>();

would initialize the Set and

List<String> list = new ArrayList<>();

would initialize the List. Still there is another option:

String[] array = new String[3];

would initialize new array of size 3. Arrays are fixed length.

like image 68
xenteros Avatar answered May 30 '26 09:05

xenteros



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!