Created
June 13, 2022 04:23
-
-
Save hrit-ikkumar/9c96dae009231e46bf1cf92e6c5ff026 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
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