0

I am working on a simple Java program where we have sorted string array named arr I am trying to compare two adjacent string and calculate frequency for each string present in that array

for(j1=0;j1<arr.length;j1++){
    if(j1+1 < arr.length){ // To prevent arrayOutofBoundsException
         if(arr[j1].equals(arr[j1+1])){
              counter++;
         }
         else {
              System.out.println(arr[j1]+" "+counter);
              counter=1;
         }

But it's not working right , what's wrong ? edit:problem is not in comparing , it's not calculating frequency as desired

Sigma
  • 742
  • 2
  • 9
  • 24

4 Answers4

2

OK, besides the equals fix, you want to keep the original order of words:

String orig = "hellow hello hello how how he ho" ;
//String orig = "how are you how do you do";

String[] arr = orig.split(" ");

//Arrays.sort(arr);

for(int j1 = 0; j1 < arr.length; j1++){
    if (arr[j1] != null) {
        int counter = 1;
        for(int j2 = j1+1; j2 < arr.length; j2++) {
            if(arr[j2] != null && arr[j1].equals(arr[j2])){
                counter++;
                arr[j2] = null;
            }
        }
        System.out.println(arr[j1]+" "+counter);
    }
}

The trick is that I run through the array, count all occurrences, null the occurrences, so they don't count again, and print the count. No need to sort the array.

gaborsch
  • 15,408
  • 6
  • 37
  • 48
0

== compares object identity in terms of memory address - to compare objects in terms of equality, use the equals-method.

Smutje
  • 17,733
  • 4
  • 24
  • 41
0

This should work:

public static void main(String[] args)
    {       
        String text = "how are you how do you do";
        String[] keys = {"how", "are", "you", "how", "do", "you", "do"};
         Arrays.sort(keys);
        String[] uniqueKeys;
        int count = 0;
        System.out.println(text);
        uniqueKeys = getUniqueKeys(keys);

        for(String key: uniqueKeys)
        {
            if(null == key)
            {
                break;
            }           
            for(String s : keys)
            {
                if(key.equals(s))
                {
                    count++;
                }               
            }
            System.out.println("Count of ["+key+"] is : "+count);
            count=0;
        }
    }

    private static String[] getUniqueKeys(String[] keys)
    {
        String[] uniqueKeys = new String[keys.length];

        uniqueKeys[0] = keys[0];
        int uniqueKeyIndex = 1;
        boolean keyAlreadyExists = false;

        for(int i=1; i<keys.length ; i++)
        {
            for(int j=0; j<=uniqueKeyIndex; j++)
            {
                if(keys[i].equals(uniqueKeys[j]))
                {
                    keyAlreadyExists = true;
                }
            }           

            if(!keyAlreadyExists)
            {
                uniqueKeys[uniqueKeyIndex] = keys[i];
                uniqueKeyIndex++;               
            }
            keyAlreadyExists = false;
        }       
        return uniqueKeys;
    }

Output:

how are you how do you do
Count of [how] is : 2
Count of [are] is : 1
Count of [you] is : 2
Count of [do] is : 2
Zeeshan
  • 11,851
  • 21
  • 73
  • 98
-1

Are you looking some sort of this

public static void main(String[] args){
        String[] arr = new String[5];
        arr[0] =  "One";
        arr[1] =  "Two";
        arr[2] =  "One";
        arr[3] =  "Three";
        arr[4] =  "Two";

        List<String> lstString = Arrays.asList(arr);
        Collections.sort(lstString);
        for(String eachString : arr){
            System.out.println("Frequency of " + eachString + " is " + getFrequency(eachString,lstString));
        }
    }

    private static int getFrequency(String word, List lstOfString){
        int frequency = 1;
        if(lstOfString != null && lstOfString.size() > 0){
            int firstIndex = lstOfString.indexOf(word);
            int lastIndex = lstOfString.lastIndexOf(word);
            frequency += lastIndex - firstIndex;
        }
        return frequency;
    }

Result : Frequency of One is 2 Frequency of One is 2 Frequency of Three is 1 Frequency of Two is 2 Frequency of Two is 2

Kumar
  • 1,106
  • 4
  • 15
  • 33