File tree 1 file changed +17
-10
lines changed
scripts/algorithms/U/Unique Number of Occurrences
1 file changed +17
-10
lines changed Original file line number Diff line number Diff line change 1
- // Runtime: 5 ms (Top 17.42%) | Memory: 42.1 MB (Top 47.50%)
2
1
class Solution {
3
2
public boolean uniqueOccurrences (int [] arr ) {
3
+ Arrays .sort (arr );
4
+ HashSet <Integer > set = new HashSet <>();
4
5
5
- var hmap = new HashMap <Integer ,Integer >();
6
- for (int i :arr ){
7
- hmap .put (i ,hmap .getOrDefault (i ,0 )+1 );
8
- }
9
- var arrli =new ArrayList <Integer >(hmap .keySet ());
10
- var set =new HashSet <Integer >();
6
+ int c = 1 ;
7
+ for (int i = 1 ; i < arr .length ; i ++)
8
+ {
9
+ if (arr [i ] == arr [i -1 ]) c ++;
10
+
11
+ else
12
+ {
13
+ if (set .contains (c )) return false ;
11
14
12
- for (Integer i : arrli )
13
- set .add (hmap .get (i ));
15
+ set .add (c );
16
+
17
+ c = 1 ;
18
+ }
19
+ }
14
20
15
- return set .size ()== arrli . size () ? true : false ;
21
+ if ( set .contains ( c )) return false ;
16
22
23
+ return true ;
17
24
}
18
25
}
You can’t perform that action at this time.
0 commit comments