Skip to content

Commit b96a3cf

Browse files
committed
Sync LeetCode submission - Difference Between Element Sum and Digit Sum of an Array (rust)
1 parent 3726906 commit b96a3cf

File tree

1 file changed

+23
-0
lines changed
  • my-folder/problems/difference_between_element_sum_and_digit_sum_of_an_array

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
impl Solution {
2+
pub fn difference_of_sum(nums: Vec<i32>) -> i32 {
3+
let element_sum: i32 = nums.iter().sum();
4+
5+
let digit_sum: i32 = nums.into_iter().map(Solution::number_digit_sum).sum();
6+
7+
return (element_sum - digit_sum).abs();
8+
}
9+
10+
#[inline(always)]
11+
fn number_digit_sum(mut num: i32) -> i32 {
12+
let mut total = 0;
13+
loop {
14+
total += num % 10;
15+
num /= 10;
16+
17+
if num == 0 {
18+
break;
19+
}
20+
}
21+
total
22+
}
23+
}

0 commit comments

Comments
 (0)