Find duplicate characters in a String and count the number of occurances using Java

前端 未结 30 2466
遇见更好的自我
遇见更好的自我 2020-12-14 11:47

How can I find the number of occurrences of a character in a string?

For example: The quick brown fox jumped over the lazy dog.

Some example

相关标签:
30条回答
  • 2020-12-14 12:43
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Scanner;
    import java.util.Set;
    public class DuplicateCountChar{
    
        public static void main(String[] args) {
            Scanner inputString = new Scanner(System.in);
            String token = inputString.nextLine();
            char[] ch = token.toCharArray();
            Map<Character, Integer> dupCountMap =  new HashMap<Character,Integer>();
    
            for (char c : ch) {
                if(dupCountMap.containsKey(c)) { 
                    dupCountMap.put(c, dupCountMap.get(c)+1);
                }else {
                    dupCountMap.put(c, 1);
                }
            }
    
    
            for (char c : ch) {
                System.out.println("Key = "+c+ "Value : "+dupCountMap.get(c));
            }
    
            Set<Character> keys = dupCountMap.keySet();
            for (Character character : keys) {
                System.out.println("Key = "+character+ " Value : " + dupCountMap.get(character));
            }
    
        }**
    
    0 讨论(0)
  • 2020-12-14 12:43
    public static void main(String[] args) {
            char[] array = "aabsbdcbdgratsbdbcfdgs".toCharArray();
            char[][] countArr = new char[array.length][2];
            int lastIndex = 0;
            for (char c : array) {
                int foundIndex = -1;
                for (int i = 0; i < lastIndex; i++) {
                    if (countArr[i][0] == c) {
                        foundIndex = i;
                        break;
                    }
                }
                if (foundIndex >= 0) {
                    int a = countArr[foundIndex][1];
                    countArr[foundIndex][1] = (char) ++a;
                } else {
                    countArr[lastIndex][0] = c;
                    countArr[lastIndex][1] = '1';
                    lastIndex++;
                }
            }
            for (int i = 0; i < lastIndex; i++) {
                System.out.println(countArr[i][0] + " " + countArr[i][1]);
            }
        }
    
    0 讨论(0)
  • 2020-12-14 12:44
    public class dublicate 
    {
    public static void main(String...a)
    {
        System.out.print("Enter the String");
        Scanner sc=new Scanner(System.in);
        String st=sc.nextLine();
        int [] ar=new int[256];
        for(int i=0;i<st.length();i++)
        {
            ar[st.charAt(i)]=ar[st.charAt(i)]+1;
        }
        for(int i=0;i<256;i++)
        {
            char ch=(char)i;
            if(ar[i]>0)
            {
                if(ar[i]==1)
                {
                    System.out.print(ch);
                }
                else
                {
                    System.out.print(ch+""+ar[i]);
                }
            }
        }
    
    }
    }
    
    0 讨论(0)
  • 2020-12-14 12:44
      public class StringCountwithOutHashMap {
      public static void main(String[] args) {
        System.out.println("Plz Enter Your String: ");
        Scanner sc = new Scanner(System.in);
        String s1 = sc.nextLine();
        int count = 0;
        for (int i = 0; i < s1.length(); i++) {
            for (int j = 0; j < s1.length(); j++) {
                if (s1.charAt(i) == s1.charAt(j)) {
                    count++;
                } 
            } 
            System.out.println(s1.charAt(i) + " --> " + count);
            String d = String.valueOf(s1.charAt(i)).trim();
            s1 = s1.replaceAll(d, "");
            count = 0;
        }}}
    
    0 讨论(0)
  • 2020-12-14 12:45

    Java 8 way:

    "The quick brown fox jumped over the lazy dog."
            .chars()
            .mapToObj(i -> (char) i)
            .collect(Collectors.groupingBy(Object::toString, Collectors.counting()));
    
    0 讨论(0)
  • 2020-12-14 12:45

    Use google guava Multiset<String>.

    Multiset<String> wordsMultiset = HashMultiset.create();
    wordsMultiset.addAll(words);
    for(Multiset.Entry<E> entry:wordsMultiset.entrySet()){
         System.out.println(entry.getElement()+" - "+entry.getCount());
    }
    
    0 讨论(0)
提交回复
热议问题