Created
June 27, 2017 01:44
-
-
Save thmain/cfb6ef600cea630da64cfe8a773662b3 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
import java.util.HashMap; | |
import java.util.Iterator; | |
import java.util.Map; | |
public class MaxRepeatingUsingHashMap { | |
public void maxRepeatingElementUsingMap(int [] arrA){ | |
//Will store each character and it's count | |
HashMap<Integer, Integer> map = new HashMap<>(); | |
for (int i = 0; i <arrA.length; i++) { | |
if(map.containsKey(arrA[i])){ | |
map.put(arrA[i],map.get(arrA[i])+1); | |
}else{ | |
map.put(arrA[i], 1); | |
} | |
} | |
//traverse the map and track the element which has max count | |
Iterator entries = map.entrySet().iterator(); | |
int maxCount = 0; | |
int element =arrA[0]; | |
while(entries.hasNext()){ | |
Map.Entry entry = (Map.Entry) entries.next(); | |
int count = (Integer)entry.getValue(); | |
if(maxCount<count){ | |
maxCount = count; | |
element = (Integer)entry.getKey(); | |
} | |
} | |
System.out.println("Element repeating maximum no of times: " + element + ", maximum count: " + maxCount); | |
} | |
public static void main(String[] args) { | |
int [] arrA = {4, 1, 5, 2, 1, 5, 9, 8, 6, 5, 3, 2, 4, 7}; | |
MaxRepeatingUsingHashMap m = new MaxRepeatingUsingHashMap(); | |
m.maxRepeatingElementUsingMap(arrA); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment