Search code examples
javastringindexofstring-lengthcharat

duplicate characters count in java without using hashmap


Please help me writing a code using string (or StringBuffer) in java. Pls don't use any other datastructure rather than String or StringBuffer. I got the answer but the code counts the characters again from starting till it has 0 characters. o/p is given at the end pls check it. Thanks in advance.

    import java.util.*;

public class DuplicateWordsOcc {
    static Scanner sc = new Scanner(System.in);
    int i,j,k;
    void show(String s,char ch,int n){
        for(i=0;i<s.indexOf(ch);i++){
          if(s.charAt(i)!=ch){
            System.out.println(ch+" : "+n);
          }
        }
    }

    public static void main(String args[]){
        DuplicateWordsOcc ob1 =new DuplicateWordsOcc();
        System.out.println("Enter the string: ");
        String s1 = sc.nextLine();
        String s2 = new String(s1);
            int i,j,k;
            for(i=0;i<s1.length();i++){
                    int count=1;
            for(j=i+1;j<s1.length();j++){
                if(s1.charAt(i)==s1.charAt(j)){
                    count++;
                    ob1.show(s1,s1.charAt(i),count);
                }
            }
        }
    }
}

Output

Enter the string:

Hello

l : 2

Here is another Output

Output

Enter the string:

Hello Hello Hello Hello

l : 2

l : 3

l : 4

l : 5

l : 6

l : 7

l : 8

l : 2

l : 3

l : 4

l : 5

l : 6

l : 7

o : 2

o : 2

o : 2

o : 3

o : 3

o : 3

o : 4

o : 4

o : 4

: 2

: 2

: 2

: 2

: 3

: 3

: 3

: 3

l : 2

l : 3

l : 4

l : 5

l : 6

l : 2

l : 3

l : 4

l : 5

o : 2

o : 2

o : 2

o : 3

o : 3

o : 3

: 2

: 2

: 2

: 2

l : 2

l : 3

l : 4

l : 2

l : 3

o : 2

o : 2

o : 2

l : 2

In the Second output pls note the letters in bold that is the real answer which shows in the very first iteration.I want to stop the execution after getting that.

From the 1st output i came to conclusion that more than count 2 starts making problems.

I know the problem is at the for loop inside show() method pls correct my code.

Pardon my English.


Solution

  • Here is the modificated code:

    import java.util.*;
    
    public class DuplicateWordsOcc {
        static Scanner sc = new Scanner(System.in);
    
        public static void main(String[] args){
            System.out.println("Enter the string: ");
            String s1 = sc.nextLine();
            s1 = s1.replaceAll("\\s","").toUpperCase();
            int len = s1.length();
            while(len > 0) {
             int count = 1;            
             for(int j=1;j<len;j++){
                if(s1.charAt(0)==s1.charAt(j)){
                    count++;
                }
             }
             if (count > 1) {
                System.out.println(s1.charAt(0)+" : "+count);    
             }
    
             String character = String.valueOf(s1.charAt(0)).trim();
             s1 = s1.replaceAll(character,"");
             len -= count;
    
           }
        }
    }
    

    Output from "He is a very good athlete and he can swim." is

    H : 3
    E : 5
    I : 2
    S : 2
    A : 4
    O : 2
    D : 2
    T : 2
    N : 2