Skip to content

Instantly share code, notes, and snippets.

@ritik-agrawal
Created September 17, 2023 07:17
Show Gist options
  • Save ritik-agrawal/648f97e44e84126687b7ad3414a1bdb7 to your computer and use it in GitHub Desktop.
Save ritik-agrawal/648f97e44e84126687b7ad3414a1bdb7 to your computer and use it in GitHub Desktop.
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;
}
}
@ritik-agrawal
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment