Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hrit-ikkumar/9c96dae009231e46bf1cf92e6c5ff026 to your computer and use it in GitHub Desktop.
Save hrit-ikkumar/9c96dae009231e46bf1cf92e6c5ff026 to your computer and use it in GitHub Desktop.
public class Solution {
public ArrayList<Integer> findOccurences(ArrayList<Integer> A) {
// create map of whole array
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
for(int x: A) map.merge(x, 1, Integer::sum);
ArrayList<Integer> result = new ArrayList<>();
// sort the array for increasing order
Collections.sort(A);
// iterate through whole array to get the count and set its count to 0 we have accessed the element
for(int x: A) {
if(map.containsKey(x) == true) {
result.add(map.get(x));
map.remove(x);
}
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment