Let's assume text
is a String and contains a text. tags
is an Array of Strings.
text = <<-EOS
Lorem ipsum dolor sit amet, consectetur adipisicing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris
nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit
esse cillum dolore eu fugiat nulla pariatur.
Excepteur sint occaecat cupidatat non proident,
sunt in culpa qui officia deserunt mollit anim id est laborum.
EOS
tags = [ "some", "in", "dolor", "laborum", "missing" ]
The algorithm should return all tags which are contained at least once in text
. In the example above
[ "in", "dolor", "laborum" ]
The resulting array is not required to be sorted. Also, I don't actually need to know the number of occurrences of each tag in text
.
I came with a few solutions, but none of them really convinced me. Any suggestion?