Finding repeated words on a string and counting the repetitions

后端 未结 29 918
梦谈多话
梦谈多话 2021-02-05 17:49

I need to find repeated words on a string, and then count how many times they were repeated. So basically, if the input string is this:

String s = \"House, House         


        
相关标签:
29条回答
  • 2021-02-05 18:27

    Using java8

    private static void findWords(String s, List<String> output, List<Integer> count){
        String[] words = s.split(", ");
        Map<String, Integer> map = new LinkedHashMap<>();
        Arrays.stream(words).forEach(e->map.put(e, map.getOrDefault(e, 0) + 1));
        map.forEach((k,v)->{
            output.add(k);
            count.add(v);
        });
    }
    

    Also, use a LinkedHashMap if you want to preserve the order of insertion

    private static void findWords(){
        String s = "House, House, House, Dog, Dog, Dog, Dog";
        List<String> output = new ArrayList<>();
        List<Integer> count = new ArrayList<>();
        findWords(s, output, count);
        System.out.println(output);
        System.out.println(count);
    }
    

    Output

    [House, Dog]
    [3, 4]
    
    0 讨论(0)
  • 2021-02-05 18:29

    Once you have got the words from the string it is easy. From Java 10 onwards you can try the following code:

    import java.util.Arrays;
    import java.util.stream.Collectors;
    
    public class StringFrequencyMap {
        public static void main(String... args) {
            String[] wordArray = {"House", "House", "House", "Dog", "Dog", "Dog", "Dog"};
            var freq = Arrays.stream(wordArray)
                             .collect(Collectors.groupingBy(x -> x, Collectors.counting()));
            System.out.println(freq);
        }
    }
    

    Output:

    {House=3, Dog=4}
    
    0 讨论(0)
  • 2021-02-05 18:29
    //program to find number of repeating characters in a string
    //Developed by Subash<subash_senapati@ymail.com>
    
    
    import java.util.Scanner;
    
    public class NoOfRepeatedChar
    
    {
    
       public static void main(String []args)
    
       {
    
    //input through key board
    
    Scanner sc = new Scanner(System.in);
    
    System.out.println("Enter a string :");
    
    String s1= sc.nextLine();
    
    
        //formatting String to char array
    
        String s2=s1.replace(" ","");
        char [] ch=s2.toCharArray();
    
        int counter=0;
    
        //for-loop tocompare first character with the whole character array
    
        for(int i=0;i<ch.length;i++)
        {
            int count=0;
    
            for(int j=0;j<ch.length;j++)
            {
                 if(ch[i]==ch[j])
                    count++; //if character is matching with others
            }
            if(count>1)
            {
                boolean flag=false;
    
                //for-loop to check whether the character is already refferenced or not 
                for (int k=i-1;k>=0 ;k-- )
                {
                    if(ch[i] == ch[k] ) //if the character is already refferenced
                        flag=true;
                }
                if( !flag ) //if(flag==false) 
                    counter=counter+1;
            }
        }
        if(counter > 0) //if there is/are any repeating characters
                System.out.println("Number of repeating charcters in the given string is/are " +counter);
        else
                System.out.println("Sorry there is/are no repeating charcters in the given string");
        }
    }
    
    0 讨论(0)
  • 2021-02-05 18:29

    I hope this will help you

    public void countInPara(String str) {

        Map<Integer,String> strMap = new HashMap<Integer,String>();
        List<String> paraWords = Arrays.asList(str.split(" "));
        Set<String> strSet = new LinkedHashSet<>(paraWords);
        int count;
    
        for(String word : strSet) {
            count = Collections.frequency(paraWords, word);
            strMap.put(count, strMap.get(count)==null ? word : strMap.get(count).concat(","+word));
        }
    
        for(Map.Entry<Integer,String> entry : strMap.entrySet())
            System.out.println(entry.getKey() +" :: "+ entry.getValue());
    }
    
    0 讨论(0)
  • 2021-02-05 18:29
    import java.util.HashMap;
    import java.util.Scanner;
    public class class1 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String inpStr = in.nextLine();
        int key;
    
        HashMap<String,Integer> hm = new HashMap<String,Integer>();
        String[] strArr = inpStr.split(" ");
    
        for(int i=0;i<strArr.length;i++){
            if(hm.containsKey(strArr[i])){
                key = hm.get(strArr[i]);
                hm.put(strArr[i],key+1);
    
            }
            else{
                hm.put(strArr[i],1);
            }   
        }
        System.out.println(hm);
    }
    

    }

    0 讨论(0)
提交回复
热议问题