Skip to content

Commit d4bc7da

Browse files
authored
Refactored Distribute Candies.java
1 parent 363aadc commit d4bc7da

File tree

1 file changed

+6
-13
lines changed

1 file changed

+6
-13
lines changed

Easy/Distribute Candies.java

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
11
class Solution {
2-
public int distributeCandies(int[] candies) {
3-
Map<Integer, Integer> count = new HashMap<>();
4-
int unique = 0;
5-
int n = candies.length;
6-
for (int i=0;i<n;i++) {
7-
if (!count.containsKey(candies[i])) {
8-
unique++;
9-
}
10-
count.put(candies[i],1);
11-
}
12-
13-
return unique > n/2 ? n/2 : unique;
14-
}
2+
public int distributeCandies(int[] candyType) {
3+
return Math.min(
4+
Arrays.stream(candyType).boxed().collect(Collectors.toSet()).size(),
5+
candyType.length / 2
6+
);
7+
}
158
}

0 commit comments

Comments
 (0)