Finding repeated words on a string and counting the repetitions

后端 未结 29 917
梦谈多话
梦谈多话 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:24

        public static void main(String[] args){
        String string = "elamparuthi, elam, elamparuthi";
        String[] s = string.replace(" ", "").split(",");
        String[] op;
        String ops = "";
    
        for(int i=0; i<=s.length-1; i++){
            if(!ops.contains(s[i]+"")){
                if(ops != "")ops+=", "; 
                ops+=s[i];
            }
    
        }
        System.out.println(ops);
    }
    

提交回复
热议问题