All methods are static but it obvious why, just for example. There is also one method called getHashMap which takes a piece of work and on given array returns hashmap with elemnts and its count number.
public static void main(String[] args) {
int[] input = {2, 3, 4, 4, 4, 3, 4, 3, 3, 6, 4, 2, 2, 2, 7, 2, 3, 3};
System.out.println("dominator: " + calDom(input));
}
static public Map<Integer, Integer> getHashMap(int[] B) {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i = 0; i < B.length; i++) {
if (map.containsKey(B[i])) {
int b = map.get(B[i]);
map.remove(B[i]);
map.put(B[i], ++b);
} else {
map.put(B[i], 1);
}
}
return map;
};
static public int calDom(int[] A) {
int dominator = 0;
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
map = getHashMap(A);
System.out.println("map " + map);
int val = 0;
int maxOcc = 0;
for (Iterator it = map.keySet().iterator(); it.hasNext();) {
val = (Integer) it.next();
//System.out.println(map.get(val) + " " + maxOcc);
if (map.get(val) > maxOcc) {
dominator = val;
maxOcc = (Integer) map.get(val);
}
}
int k = 0;
for (Iterator it = map.keySet().iterator(); it.hasNext();) {
val = (Integer) it.next();
if (map.get(val).equals(maxOcc)) {
k++;
}
System.out.println(k);
if (k > 1) {
return -1;
}
}
System.out.println("dominator: " + map.get(dominator) + " size: " + A.length);
return dominator;
}
You may ask me why do I iterate twice that map. (Already my friend asked me this q) That is I couldn’t find better solution for cases where your map looks like this:
map {2=5, 3=6, 4=5, 6=1, 7=1} . The code above works fine and returns -1 only when there is no dominator. But in case I leave one iteration and put another condition “dominator == val ? -1 ” then it won’t work in case of map {2=5, 3=5, 4=6, 6=1, 7=1}. Because it will return -1 although there is another candidate for being dominator.
You can preview also another great solution for this issue here:
package sunjavaforum;
import java.util.*;
public class Thread5365251{
public static void main(String[] args){
//int[] input={3, 4, 3, 2, 3, -1, 3, 3};
int[] input={3, 4, 3, 4, 3, 4, 3, 3};
new Thread5365251().calculateDominator(input);
}
public void calculateDominator(int[] input){
int dominator=0;
int maxOccurrence=0;
Map<Integer,Entry> elements=new HashMap<Integer, Entry>();
// Calculate occurrences of all elements.
for(int iCount=0; iCount<input.length; iCount++){
if(elements.containsKey(input[iCount])){
elements.get(input[iCount]).addIndex(iCount);
}else{
elements.put(input[iCount], new Entry(input[iCount],iCount));
}
}
int val=0;
// Find the dominator.
for(Iterator it=elements.keySet().iterator();it.hasNext();){
val=(Integer)it.next();
if(elements.get(val).getNumberOfOccurrences()> maxOccurrence){
dominator=val;
maxOccurrence=elements.get(val).getNumberOfOccurrences();
}
}
if(maxOccurrence > (input.length/2)){
System.out.println("Dominator: "+dominator);
System.out.println("Number of occurrences of dominator: "+maxOccurrence);
System.out.println("Index of occurrences of dominator: "+elements.get(dominator).getIndexes());
} else{
System.out.println("There is no dominator. Result is -1");
}
}
/**
* A class to hold all occurreces of each distinct element.
*/
private class Entry{
private int val;
private List<Integer> indexes;
public Entry(int mVal, int index){
this.val=mVal;
this.indexes=new ArrayList<Integer>();
this.indexes.add(index);
}
public void setValue(int mVal){
this.val=mVal;
}
public int getValue(){
return this.val;
}
public List<Integer> getIndexes(){
return this.indexes;
}
public int getNumberOfOccurrences(){
return this.indexes.size();
}
public void addIndex(int index){
this.indexes.add(index);
}
}
}