File tree Expand file tree Collapse file tree 1 file changed +18
-8
lines changed
src/main/java/g1201_1300/s1207_unique_number_of_occurrences Expand file tree Collapse file tree 1 file changed +18
-8
lines changed Original file line number Diff line number Diff line change 11package 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 ;
65import java .util .HashMap ;
7- import java .util .HashSet ;
86import java .util .Map ;
9- import java .util .Set ;
107
118public 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}
You can’t perform that action at this time.
0 commit comments