java - Not printing occurrences of each keys correctly with the HashMap -


i trying print how many times particular key has occurred in array , seems it's printing 1 of values. please tell me logical error having in code below:

import java.util.hashmap; import java.util.map;  public class mostoccuranceofnumber {      public static void main(string[] args) {           int[] n = {1,2,3,4,5,6,7,7,7,7};          map<integer, integer> map = new hashmap<integer,integer>();          // create hash map          for(int = 0 ; < n.length ; i++){               if(map.containskey(n)){                  map.put(n[i], map.get(n[i]) +1);              }             else{                  map.put(n[i], 1);             }              for(map.entry<integer, integer> m : map.entryset()){                  system.out.println("key "+m.getkey()+"occured"+m.getvalue()+"times");              }           }      }  } 

try this:

import java.util.hashmap; import java.util.map;  public class mostoccuranceofnumber {  public static void main(string[] args) {       int[] n = {1,2,3,4,5,6,7,7,7,7};      map<integer, integer> map = new hashmap<integer,integer>();      // create hash map      for(int = 0 ; < n.length ; i++){           if(map.containskey(n[i])){//you made mistake here              map.put(n[i], map.get(n[i]) +1);          }         else{              map.put(n[i], 1);         }        }       } for(map.entry<integer, integer> m : map.entryset()){              system.out.println("key  "+m.getkey()+"occured "+m.getvalue()+" times ");//sorry forgot take outside    }  } 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -