Advertisement
AcidRogue

Some test

Jun 18th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.83 KB | None | 0 0
  1.  public static int count(int times, int array[]){
  2.         Map<Integer, Integer> map = new HashMap<>();
  3.  
  4.         for (int i = 0; i < array.length; i++) {
  5.             //If the map doesn't contain the value of array[i], add a Key Value to the map;
  6.             if(!map.containsKey(array[i])){
  7.                 map.put(array[i], 1);
  8.             }
  9.             //If the map already has array[i], increase value by one
  10.             else{
  11.                 map.put(array[i], map.get(array[i]) + 1);
  12.             }
  13.         }
  14.  
  15.         for (int i = 0; i < map.size(); i++) {
  16.             if(map.get(array[i]) == times){
  17.                 return array[i];
  18.             }
  19.         }
  20.  
  21.         //No such value exist
  22.         return -1;
  23.     }
  24.  
  25.     public static void main(String[] args) {
  26.         System.out.println(count(2, new int[]{7,2,3,3}));
  27.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement