my code stock.com

Snippet options

Download: Download snippet as maxocc.java.
Copy snippet: For this you need a free my code stock.com account.
Embed code : You will find the embed code for this snippet at the end of the page, if you want to embed it into a website or a blog!

/*
 * CountClass implements counting the largest occurring consecutive element
 */
import java.util.HashMap;
import java.util.Map; 
import java.util.ArrayList;
 
class MyException extends Exception {
    public MyException (String msg) {
	    super(msg);
    }
}
 
public class CountClass {
    public static void main(String[] args) throws MyException {
	    //to put a try block
		try {
		    String aString = args[0];
	        countCharOccr(args[0]);
		} catch(Exception e) {
		    throw new MyException("Argument too less");
		}
	}
	
	/*
	 * @return void
	 */
	public static void countCharOccr(String pString) {
	    Map<Character, Integer> aFreq = new HashMap<Character, Integer>();
		
		char aLookup = '\0';
		int aCount = 1;
	    //for (char ch : pString.toCharArray()) {
		for (int i = 0; i < pString.length(); i++) {
		    
			if (aLookup == pString.charAt(i)) {
			    aCount++;
				continue;
			    //aFreq.put(ch, count + 1);
			} else {
			   
			
			    int aValue = aFreq.containsKey(aLookup) ? aFreq.get(aLookup) : 0;
	
                if (aCount > aValue && aLookup != '\0') 
                    aFreq.put(aLookup, aCount);
                aCount = 1;			
                aLookup = pString.charAt(i);				
			}
		}
		
		
		//checking for the last character
		int aValue = aFreq.containsKey(aLookup) ? aFreq.get(aLookup) : 0;
		if (aCount > aValue)
		    aFreq.put(aLookup, aCount);
		
		for (Map.Entry<Character, Integer> entry : aFreq.entrySet()) {
            System.out.println(entry.getKey()+" : "+entry.getValue());
		}
		
		ArrayList<String> myList = new ArrayList<>();
		Character maxKey = null;
		Integer maxValue = Integer.MIN_VALUE;
		
		int j = 0;
		String aString = null;
		for (Map.Entry<Character, Integer> entry : aFreq.entrySet()) {
		    if (entry.getValue() > maxValue) {
			    myList.clear();
			    maxValue = entry.getValue();
				maxKey = entry.getKey();
				
                aString = "'" + maxKey + "' for " + maxValue + " times ";
				myList.add(aString);
		    } else if (entry.getValue() == maxValue) {
			    maxValue = entry.getValue();
				maxKey = entry.getKey();
			    aString = "'" + maxKey + "' for " + maxValue + " times ";
			    myList.add(aString);
			}
		}
		System.out.println("\nThe largest occuring letter: \n");
		for (String s : myList) 
		    System.out.println(s);
		//System.out.println("The largest occuring letter is '" + maxKey + "' for " + maxValue + " times ");
 	}
}

Create a free my code stock.com account now.

my code stok.com is a free service, which allows you to save and manage code snippes of any kind and programming language. We provide many advantages for your daily work with code-snippets, also for your teamwork. Give it a try!

Find out more and register now

You can customize the height of iFrame-Codes as needed! You can find more infos in our API Reference for iframe Embeds.