|
|
@ -0,0 +1,46 @@ |
|
|
|
#![allow(unused)]
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
use std::collections::BTreeSet;
|
|
|
|
|
|
|
|
let a1 = vec!["foo", "live", "", "strong"];
|
|
|
|
let a2 = vec!["arp", "live", "strong"];
|
|
|
|
let a3 = vec!["live", "arp", "foo", "strong", "arp"];
|
|
|
|
let b1 = vec!["lively", "alive", "harp", "sharp", "armstrong"];
|
|
|
|
|
|
|
|
println!("\n");
|
|
|
|
|
|
|
|
print!("1: {:?}\t", in_array(&a1,&b1));
|
|
|
|
println!("{:?}\n", in_array(&a3,&b1));
|
|
|
|
|
|
|
|
print!("2: {:?}\t", in_array2(&a1,&b1));
|
|
|
|
println!("{:?}\n", in_array2(&a3,&b1));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
fn in_array(arr_a: &[&str], arr_b: &[&str]) -> Vec<String> {
|
|
|
|
let mut arr: Vec<_> = arr_a.iter()
|
|
|
|
.filter(|e| !e.is_empty())
|
|
|
|
.copied().collect();
|
|
|
|
arr.sort();
|
|
|
|
arr.dedup();
|
|
|
|
|
|
|
|
let mut res = vec![];
|
|
|
|
for a in arr.iter() {
|
|
|
|
if arr_b.iter().any(|&b| b.contains(a)) {
|
|
|
|
res.push(a.to_string());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
|
|
|
fn in_array2(arr_a: &[&str], arr_b: &[&str]) -> Vec<String> {
|
|
|
|
use std::collections::BTreeSet;
|
|
|
|
let arr: BTreeSet<_> = arr_a.iter().copied().collect();
|
|
|
|
|
|
|
|
arr.iter()
|
|
|
|
.filter(|&a| !a.is_empty())
|
|
|
|
.filter(|&a| arr_b.iter().any(|&b| b.contains(a)))
|
|
|
|
.map(|a| a.to_string())
|
|
|
|
.collect::<Vec<String>>()
|
|
|
|
}
|