Last active
February 5, 2021 17:56
-
-
Save fricze/cc8df2329b7a569e932ef0ae6d4d16b2 to your computer and use it in GitHub Desktop.
count ordered elements in vector
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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