Skip to content

Commit 25dac24

Browse files
authored
Added solution for 2148
1 parent 0b8d673 commit 25dac24

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use std::cmp::max;
2+
3+
impl Solution {
4+
// Loop through nums and keep track of the largest and smallest nums and how frequently they occur. Then return the length of nums subtracted by the frequencies of largest and smallest numbers
5+
pub fn count_elements(nums: Vec<i32>) -> i32 {
6+
let mut smallest = i32::MAX;
7+
let mut largest = i32::MIN;
8+
let mut smallest_freq = 0;
9+
let mut largest_freq = 0;
10+
11+
for num in &nums {
12+
if smallest > *num {
13+
smallest = *num;
14+
smallest_freq = 1;
15+
} else if smallest == *num {
16+
smallest_freq += 1;
17+
}
18+
19+
if largest < *num {
20+
largest = *num;
21+
largest_freq = 1;
22+
} else if largest == *num {
23+
largest_freq += 1;
24+
}
25+
}
26+
return max((nums.len() as i32 - smallest_freq - largest_freq), 0)
27+
}
28+
}

0 commit comments

Comments
 (0)