Created
September 17, 2023 07:17
-
-
Save ritik-agrawal/648f97e44e84126687b7ad3414a1bdb7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public boolean uniqueOccurrences(int[] arr) { | |
Arrays.sort(arr); | |
var seen = new HashMap<Integer, List<Integer>>(); | |
var s = 0; | |
var e = 0; | |
var len = arr.length; | |
while (e < len){ | |
while ( e < len && arr[s] == arr[e]){ | |
e++; | |
} | |
var key = (e-s); | |
var values = seen.getOrDefault(key, new ArrayList<Integer>()); | |
if (!values.isEmpty()){ | |
return false; | |
} | |
values.add(arr[s]); | |
seen.put(key, values); | |
s = e; | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Leet code
Link of the question here
Achievement
The code submitted beats 97% of the total submissions in terms of runtime with 2 ms as runtime and beats 21% of total submissions in terms of space complexity.