Logo

Programming-Idioms

  • D
  • Fortran
  • C
  • Pascal
  • Erlang

Idiom #218 List intersection

Create the list c containing all unique elements that are contained in both lists a and b.
c should not contain any duplicates, even if a and b do.
The order of c doesn't matter.

C = lists:flatten([A -- B] ++ [B -- A]).
uses Classes;
for elem in a do
  if (b.indexof(elem) >= 0) and (c.indexof(elem) = -1) then
    c.add(elem);
(def c (clojure.set/intersection (set a) (set b)))

New implementation...
< >
programming-idioms.org