Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- HashMap<Character,Integer> map = new HashMap<Character,Integer>();
- String s = "aasjjikkk";
- for(int i = 0; i < s.length(); i++){
- char c = s.charAt(i);
- Integer val = map.get(new Character(c));
- if(val != null){
- map.put(c, new Integer(val + 1));
- }else{
- map.put(c,1);
- }
- }
- Multiset<Character> chars = HashMultiset.create();
- for (int i = 0; i < string.length(); i++) {
- chars.add(string.charat(i));
- }
- public class Pair
- {
- private char letter;
- private int count;
- public Pair(char letter, int count)
- {
- this.letter = letter;
- this.count= count;
- }
- public char getLetter(){return key;}
- public int getCount(){return count;}
- }
- public static Pair countCharFreq(String s)
- {
- String temp = s;
- java.util.List<Pair> list = new java.util.ArrayList<Pair>();
- while(temp.length() != 0)
- {
- list.add(new Pair(temp.charAt(0), countOccurrences(temp, temp.charAt(0))));
- temp.replaceAll("[" + temp.charAt(0) +"]","");
- }
- }
- public static int countOccurrences(String s, char c)
- {
- int count = 0;
- for(int i = 0; i < s.length(); i++)
- {
- if(s.charAt(i) == c) count++;
- }
- return count;
- }
- Hashtable<Character,Integer> table = new Hashtable<Character,Integer>();
- String str = "aasjjikkk";
- for( c in str ) {
- if( table.get(c) == null )
- table.put(c,1);
- else
- table.put(c,table.get(c) + 1);
- }
- for( elem in table ) {
- println "elem:" + elem;
- }
Add Comment
Please, Sign In to add comment