I am looking for time-efficient ways to compare elements between arrays. I have two simplified short examples of the data my arrays could contain:
let a = ["mushrooms", "bread", "tomato", "sauce"]
let b = ["o", "e", "a"]
The lengths of the arrays I am working with exceed 500 000 elements. In my arrays I would be comparing every element from array b
to every element in array a
and computing a result depending on that. (i.e. checking how many O
s the words from array a
have and storing the result) However, I am currently using two loops - one iterating a
and a nested one inside it iterating b
.
My goal is to improve efficiency since I believe my algorithm is far from being time-efficient. I would love to learn about common practices which deal with this in a better way.