Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Rust

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.

use std::collections::HashSet;
let set_a: HashSet<_> = a.into_iter().collect();
let set_b: HashSet<_> = b.into_iter().collect();
let c = set_a.intersection(&set_b);
use std::collections::HashSet;
let unique_a = a.iter().collect::<HashSet<_>>();
let unique_b = b.iter().collect::<HashSet<_>>();

let c = unique_a.intersection(&unique_b).collect::<Vec<_>>();
(def c (clojure.set/intersection (set a) (set b)))

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