Code to find a repeated number in an array
Anonymous
public void CountTheRepeatedNumber(int[] arr){ Map map=new Hashmap(); for(Integer num:arr){ if(map.containsKey(num)){ map.put(num,map.get(num)+1); //increase the value if it is repeated } else{ map.put(num,1); // put 1 into value if number is first time present } } Set key=map.KeySet(); //return you all the distinct integer number for(Integer i:key){ if(map.get(i)>=0){ syso(i +" is " +map.get(i) +"Times in an array"); //simply print this so u will get repeated numbers with count and just print it } }
Check out your Company Bowl for anonymous work chats.