Skip to content

Instantly share code, notes, and snippets.

@fricze
Last active February 5, 2021 17:56
Show Gist options
  • Save fricze/cc8df2329b7a569e932ef0ae6d4d16b2 to your computer and use it in GitHub Desktop.
Save fricze/cc8df2329b7a569e932ef0ae6d4d16b2 to your computer and use it in GitHub Desktop.
count ordered elements in vector
fn main() {
let v1 = vec![1, 1, 1, 1, 2, 2, 1, 1, 5, 5, 5]
.into_iter()
.enumerate()
.fold(vec![], |mut coll, (idx, val)| {
if idx == 0 {
[coll, [(val, 1)].to_vec()].concat()
} else {
let last_idx = coll.len() - 1;
let (prev_val, last) = coll[last_idx];
if val == prev_val {
coll[last_idx] = (val, last + 1);
coll
} else {
[coll, [(val, 1)].to_vec()].concat()
}
}
});
dbg!(v1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment