I think @trincot's answer is correct, but I thought I would offer my own thought process to how I would solve it.
I think a good algorithm exists if we take a closer look at your problem statement:
if item of A in B ,mark the item "red" ,if not ,mark it "blue"
In pseudo code this becomes:
for(item in b):
if(a.contains(item)){
b.markRed();
}else{
b.markBlue();
}
}
This has one loop instead of two, which means we're back O(n) realm instead of the very bad O(n^2). The question then becomes, what data structure do we use for A such that there is a "contains" method? @trincot suggests a Set
but any map/dictionary implementation would also serve it's purpose.
There's the extra cost of creating the set/map/dictionary, but it's much, much less than nested loops.
So, it's faster because we replaced a loop with a constant-time lookup which means for every B, we're doing 1 operation instead of A operations.
@trincot's big-O analysis also looks pretty good if you want a more complete understanding of why its much faster.