Click here to Skip to main content
15,889,216 members
Home / Discussions / Java
   

Java

 
QuestionCellphone Banking Pin
dabrainsoftware20-May-14 1:38
dabrainsoftware20-May-14 1:38 
AnswerRe: Cellphone Banking Pin
Richard MacCutchan20-May-14 2:56
mveRichard MacCutchan20-May-14 2:56 
AnswerRe: Cellphone Banking Pin
thatraja20-May-14 4:32
professionalthatraja20-May-14 4:32 
Questionhow to run JASA (java auction simulator api) Pin
ho0olvlan17-May-14 10:29
ho0olvlan17-May-14 10:29 
AnswerRe: how to run JASA (java auction simulator api) Pin
Richard MacCutchan17-May-14 21:36
mveRichard MacCutchan17-May-14 21:36 
AnswerRe: how to run JASA (java auction simulator api) Pin
Prasad Khandekar23-May-14 2:13
professionalPrasad Khandekar23-May-14 2:13 
QuestionWrite a program to count occurances of each character in a string Pin
chiku249-May-14 9:12
chiku249-May-14 9:12 
AnswerRe: Write a program to count occurances of each character in a string Pin
Tanvir Ahmed Tanin9-May-14 16:00
Tanvir Ahmed Tanin9-May-14 16:00 
Here is the function where your get the occurrence of each character in any string with O(n) times.

public static void main(String args[]){
		String str="mam is going";
		HashMap<String, Integer> freqOfChar=calculateCharFrequency(str!=null?str.trim():"");
		printFreeOfChar(freqOfChar);
	}
	
	static HashMap<String, Integer> calculateCharFrequency(String str){
		HashMap<String, Integer> freqOfChar = new HashMap<String, Integer>();
		String ch;
		if(str!=null){
			for(int i=0;i<str.length();i++){
				ch=Character.toString(str.charAt(i)).trim();
				if(ch.length()>0){
					if(freqOfChar.get(ch)!=null){
						freqOfChar.put(ch, (freqOfChar.get(ch)+1));
					}else{
						freqOfChar.put(ch, 1);
					}
				}
			}
		}
		return freqOfChar;
	}
	
	static void printFreeOfChar(HashMap<String, Integer>  freqOfChar){
		 for (String key : freqOfChar.keySet()) {
			 System.out.println("Char: "+key+ "; Occurance: "+freqOfChar.get(key));
		 }
		
	}


output:
CSS
Char: o; Occurance: 1
Char: i; Occurance: 2
Char: m; Occurance: 2
Char: a; Occurance: 1
Char: n; Occurance: 1
Char: g; Occurance: 2
Char: s; Occurance: 1

GeneralRe: Write a program to count occurances of each character in a string Pin
TorstenH.11-May-14 21:52
TorstenH.11-May-14 21:52 
QuestionJChomboBox Pin
Kokkula7-May-14 21:49
Kokkula7-May-14 21:49 
AnswerRe: JChomboBox Pin
TorstenH.9-May-14 4:35
TorstenH.9-May-14 4:35 
GeneralRe: JChomboBox Pin
Kokkula9-May-14 19:27
Kokkula9-May-14 19:27 
GeneralRe: JChomboBox Pin
Richard MacCutchan9-May-14 21:41
mveRichard MacCutchan9-May-14 21:41 
QuestionHELP Pin
Member 107983016-May-14 1:40
Member 107983016-May-14 1:40 
SuggestionRe: HELP Pin
Richard MacCutchan6-May-14 6:00
mveRichard MacCutchan6-May-14 6:00 
GeneralRe: HELP Pin
Member 107983016-May-14 7:08
Member 107983016-May-14 7:08 
GeneralRe: HELP Pin
Richard MacCutchan6-May-14 9:06
mveRichard MacCutchan6-May-14 9:06 
AnswerRe: HELP Pin
TorstenH.6-May-14 21:26
TorstenH.6-May-14 21:26 
QuestionHow to Pin
mtouxx5-May-14 19:49
mtouxx5-May-14 19:49 
QuestionRe: How to Pin
Richard MacCutchan5-May-14 20:39
mveRichard MacCutchan5-May-14 20:39 
Questionjava Pin
Member 107878361-May-14 2:14
Member 107878361-May-14 2:14 
AnswerRe: java Pin
Richard MacCutchan1-May-14 5:34
mveRichard MacCutchan1-May-14 5:34 
Questionplease reply as soon as possible Pin
Member 107875691-May-14 0:36
Member 107875691-May-14 0:36 
AnswerRe: please reply as soon as possible Pin
Richard MacCutchan1-May-14 0:50
mveRichard MacCutchan1-May-14 0:50 
GeneralRe: please reply as soon as possible Pin
Member 107875691-May-14 17:59
Member 107875691-May-14 17:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.