0

What is an efficient way to return the common elements between multiple large arrays? The arrays would contain strings, but knowing how to do it generically would be nice too.

For example, input of ['a', 'b'], ['a', 'b', 'c'], ['b', 'c'] should return ['b'].

user1027169
  • 2,627
  • 3
  • 27
  • 50
  • Sort (copies of) the arrays, then walk through two at a time, skipping ahead in each array where you have an element that's smaller than the next element in the other array. – Jerry Coffin Feb 20 '13 at 00:15

1 Answers1

0

It depends on what you are comparing... if it's always alphabet letters than you could create arrays that have 26 slots (1 for each alphabet letter) and give them values of 0 (that letter is not in the list) or 1 (the letter is in the list)... then compile (add) the matrices together, let's say you have 5 arrays... then any slots with a value 5 would mean that the slot had a 1 in all 5 arrays, therefor that letter showed up in all 5 arrays.

Albert Renshaw
  • 17,282
  • 18
  • 107
  • 195