Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static int count(int times, int array[]){
- Map<Integer, Integer> map = new HashMap<>();
- for (int i = 0; i < array.length; i++) {
- //If the map doesn't contain the value of array[i], add a Key Value to the map;
- if(!map.containsKey(array[i])){
- map.put(array[i], 1);
- }
- //If the map already has array[i], increase value by one
- else{
- map.put(array[i], map.get(array[i]) + 1);
- }
- }
- for (int i = 0; i < map.size(); i++) {
- if(map.get(array[i]) == times){
- return array[i];
- }
- }
- //No such value exist
- return -1;
- }
- public static void main(String[] args) {
- System.out.println(count(2, new int[]{7,2,3,3}));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement