Skip to content

Commit 150edd2

Browse files
solves average salary excluding the minimum and maximum
1 parent c381709 commit 150edd2

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@
378378
| 1475 | [Final Prices With Special Discount in a Shop](https://leetcode.com/problems/final-prices-with-a-special-discount-in-a-shop) | [![Java](assets/java.png)](src/FinalPricesWithASpecialDiscountInAShop.java) | |
379379
| 1480 | [Running Sum of 1D Array](https://leetcode.com/problems/running-sum-of-1d-array) | [![Java](assets/java.png)](src/RunningSumOf1DArray.java) | |
380380
| 1486 | [XOR Operations in An Array](https://leetcode.com/problems/xor-operation-in-an-array) | [![Java](assets/java.png)](src/XOROperationInAnArray.java) | |
381-
| 1491 | [Average Salary Excluding the Minimum and Maximum Salary](https://leetcode.com/problems/average-salary-excluding-the-minimum-and-maximum-salary) | | |
381+
| 1491 | [Average Salary Excluding the Minimum and Maximum Salary](https://leetcode.com/problems/average-salary-excluding-the-minimum-and-maximum-salary) | [![Java](assets/java.png)](src/AverageSalaryExcludingTheMinimumAndMaximumSalary.java) | |
382382
| 1496 | [Path Crossing](https://leetcode.com/problems/path-crossing) | | |
383383
| 1502 | [Can Make Arithmetic Progression From Sequence](https://leetcode.com/problems/can-make-arithmetic-progression-from-sequence) | | |
384384
| 1507 | [Reformat Date](https://leetcode.com/problems/reformat-date) | | |
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import java.util.Arrays;
2+
import java.util.Set;
3+
4+
public class AverageSalaryExcludingTheMinimumAndMaximumSalary {
5+
public double average(int[] salaries) {
6+
final int maxSalary = Arrays.stream(salaries).max().getAsInt();
7+
final int minSalary = Arrays.stream(salaries).min().getAsInt();
8+
final Set<Integer> exclude = Set.of(minSalary, maxSalary);
9+
int sumSalary = 0;
10+
for (int salary : salaries) {
11+
if (!exclude.contains(salary)) sumSalary += salary;
12+
}
13+
return (double) sumSalary / (salaries.length - 2);
14+
}
15+
}

0 commit comments

Comments
 (0)