-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort-array-by-increasing-frequency.rs
62 lines (57 loc) · 1.52 KB
/
sort-array-by-increasing-frequency.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
struct Solution;
impl Solution {
pub fn frequency_sort(nums: Vec<i32>) -> Vec<i32> {
use std::collections::HashMap;
let mut map: HashMap<i32, i32> = HashMap::new();
for i in nums.iter() {
let entry = map.entry(*i).or_insert(0);
*entry += 1;
}
let mut vec = map
.into_iter()
.map(|(k, v)| (k, v))
.collect::<Vec<(i32, i32)>>();
vec.sort_by(|a, b| {
let order = a.1.cmp(&b.1);
match order {
std::cmp::Ordering::Equal => {
if a.0.cmp(&b.0).is_gt() {
std::cmp::Ordering::Less
} else {
std::cmp::Ordering::Greater
}
}
_ => order,
}
});
let mut ans = vec![];
for (v, i) in vec {
for _ in 0..i {
ans.push(v);
}
}
ans
}
}
fn main() {
println!(
"{:?}",
Solution::frequency_sort(vec![-1, 1, -6, 4, 5, -6, 1, 4, 1])
);
println!("{:?}", Solution::frequency_sort(vec![1, 1, 2, 2, 2, 3]));
}
#[cfg(test)]
mod tests {
use crate::Solution;
#[test]
fn test() {
assert_eq!(
vec![5, -1, 4, 4, -6, -6, 1, 1, 1],
Solution::frequency_sort(vec![-1, 1, -6, 4, 5, -6, 1, 4, 1])
);
assert_eq!(
vec![1, 3, 3, 2, 2],
Solution::frequency_sort(vec![2, 3, 1, 3, 2])
);
}
}