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

前端 未结 30 2465
遇见更好的自我
遇见更好的自我 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:31
    public class CountH {
    
        public static void main(String[] args) {
            String input = "Hi how are you";
    
            char charCount = 'h';
            int count = 0;
    
            input = input.toLowerCase();
    
            for (int i = 0; i < input.length(); i++) {
    
                if (input.charAt(i) == charCount) {
                    count++;
                }
    
            }
    
            System.out.println(count);
        }
    }
    
    0 讨论(0)
  • 2020-12-14 12:35

    You could use the following, provided String s is the string you want to process.

    Map<Character,Integer> map = new HashMap<Character,Integer>();
    for (int i = 0; i < s.length(); i++) {
      char c = s.charAt(i);
      if (map.containsKey(c)) {
        int cnt = map.get(c);
        map.put(c, ++cnt);
      } else {
        map.put(c, 1);
      }
    }
    

    Note, it will count all of the chars, not only letters.

    0 讨论(0)
  • 2020-12-14 12:35

    A better way would be to create a Map to store your count. That would be a Map<Character, Integer>

    You need iterate over each character of your string, and check whether its an alphabet. You can use Character#isAlphabetic method for that. If it is an alphabet, increase its count in the Map. If the character is not already in the Map then add it with a count of 1.

    NOTE: - Character.isAlphabetic method is new in Java 7. If you are using an older version, you should use Character#isLetter

        String str = "asdfasdfafk asd234asda";
        Map<Character, Integer> charMap = new HashMap<Character, Integer>();
        char[] arr = str.toCharArray();
    
        for (char value: arr) {
    
           if (Character.isAlphabetic(value)) {
               if (charMap.containsKey(value)) {
                   charMap.put(value, charMap.get(value) + 1);
    
               } else {
                   charMap.put(value, 1);
               }
           }
        }
    
        System.out.println(charMap);
    

    OUTPUT: -

    {f=3, d=4, s=4, a=6, k=1}
    
    0 讨论(0)
  • 2020-12-14 12:36
    public static void findDuplicate(String letter)
    {
    
        for(int i=0; i<letter.length();i++)
        {
            for( int j=i+1; j<letter.length();j++)
            {
                if(letter.charAt(i)==letter.charAt(j))
                {
                    System.out.println(letter.charAt(j));
                    break;
                }
            }
        }
    }
    

    findDuplicate("JAVA");

    The OUTPUT is : A

    0 讨论(0)
  • 2020-12-14 12:37
    import java.io.*;
    import java.util.*;
    import java.text.*;
    import java.math.*;
    import java.util.regex.*;
    
    public class Solution {
    
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int n = sc.nextInt();
            String reverse1;
            String reverse2;
            int count = 0;
            while(n > 0)
            {
                String A = sc.next();
                String B = sc.next();
                reverse1 = new StringBuffer(A).reverse().toString();
                reverse2 = new StringBuffer(B).reverse().toString();
                if(!A.equals(reverse1))
                {
                    for(int i = 0; i < A.length(); i++)
                    {
                        for(int j = 0; j < A.length(); j++)
                        {
                            if(A.charAt(j) == A.charAt(i))
                            {
                                count++;
                            }
                        }
                        if(count % 2 != 0)
                        {
                            A.replace(A.charAt(i),"");
                            count = 0;
                        }
                    }
                    System.out.println(A);
                }
                n--;
            } 
        }
    }
    
    0 讨论(0)
  • 2020-12-14 12:39
    public class list {
    
        public static String name(Character k){
            String s="the quick brown fox jumped over the lazy dog.";
            int count=0;
            String l1="";
            String l="";
            List<Character> list=new ArrayList<Character>();
            for(int i1=0;i1<s.length();i1++){
                list.add(s.charAt(i1));
            }
            list.sort(null);
            for (Character character : list) {
                l+=character;
            }
            for (int i1=0;i1<l.length();i1++) {
                if((l.charAt(i1)==k)){
                    count+=1;
                    l1=l.charAt(i1)+" "+Integer.toString(count);
                    if(k==' '){
                        l1="Space"+" "+Integer.toString(count);
                    }
                }else{
                    count=0;
                }
            }
            return l1;
        }
        public static void main(String[] args){
            String g = name('.');
            System.out.println(g);
        }
    }
    
    0 讨论(0)
提交回复
热议问题