Skip to content

Commit 9a9f739

Browse files
authored
Improved task 1207.
1 parent 8732d66 commit 9a9f739

File tree

1 file changed

+18
-8
lines changed
  • src/main/java/g1201_1300/s1207_unique_number_of_occurrences

1 file changed

+18
-8
lines changed
Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,29 @@
11
package g1201_1300.s1207_unique_number_of_occurrences;
22

3-
// #Easy #Array #Hash_Table #2022_03_09_Time_8_ms_(5.98%)_Space_42.4_MB_(22.63%)
3+
// #Easy #Array #Hash_Table #2022_04_29_Time_2_ms_(82.71%)_Space_42.4_MB_(34.08%)
44

5-
import java.util.Arrays;
65
import java.util.HashMap;
7-
import java.util.HashSet;
86
import java.util.Map;
9-
import java.util.Set;
107

118
public class Solution {
129
public boolean uniqueOccurrences(int[] arr) {
1310
Map<Integer, Integer> map = new HashMap<>();
14-
Arrays.stream(arr)
15-
.forEach(num -> map.put(num, map.containsKey(num) ? map.get(num) + 1 : 1));
16-
Set<Integer> set = new HashSet<>();
17-
return map.keySet().stream().mapToInt(key -> key).allMatch(key -> set.add(map.get(key)));
11+
for (int i = 0; i < arr.length; i++) {
12+
if (map.containsKey(arr[i])) {
13+
map.put(arr[i], map.get(arr[i]) + 1);
14+
} else {
15+
map.put(arr[i], 1);
16+
}
17+
}
18+
// map for check unique number of count
19+
Map<Integer, Integer> uni = new HashMap<>();
20+
for (Integer val : map.values()) {
21+
if (uni.containsKey(val)) {
22+
return false;
23+
} else {
24+
uni.put(val, 1);
25+
}
26+
}
27+
return true;
1828
}
1929
}

0 commit comments

Comments
 (0)