Efficiently finding the intersection of a variable number of sets of strings

后端 未结 7 819
旧时难觅i
旧时难觅i 2020-11-27 06:14

I have a variable number of ArrayList\'s that I need to find the intersection of. A realistic cap on the number of sets of strings is probably around 35 but could be more. I

7条回答
  •  渐次进展
    2020-11-27 06:50

    You can use single HashSet. It's add() method returns false when the object is alredy in set. adding objects from the lists and marking counts of false return values will give you union in the set + data for histogram (and the objects that have count+1 equal to list count are your intersection). If you throw the counts to TreeSet, you can detect empty intersection early.

提交回复
热议问题