Want to count occurances of Strings in Java -


so have .txt file calling using

string[] data = loadstrings("data/data.txt"); 

the file sorted , looks like:

animal animal cat cat cat dog 

i looking create algorithm count sorted list in java, without using libraries multisets or without use of maps/hashmaps. have managed far print out top occurring word so:

arraylist<string> words = new arraylist();  int[] occurrence = new int[2000]; arrays.sort(data);  (int = 0; < data.length; ++ ) { words.add(data[i]);     //put each word words arraylist } for(int =0; i<data.length; i++) {  occurrence[i] =0;  for(int j=i+1; j<data.length; j++) {    if(data[i].equals(data[j])) {      occurrence[i] = occurrence[i]+1;    }  } } int max = 0; string most_talked =""; for(int =0;i<data.length;i++) {   if(occurrence[i]>max) {     max = occurrence[i];     most_talked = data[i];   }  }  println("the talked keyword " + most_talked + " occuring " + max + " times."); 

i want rather highest occurring word perhaps top 5 or top 10. hope clear enough. reading

since said dont want use kind of data structure think can this, not performant. prefer store index rather values.

arraylist<string> words = new arraylist();  int[] occurrence = new int[2000]; arrays.sort(data);   int nwords = 0; occurrence[nwords]=1; words.add(data[0]);         (int = 1; < data.length; ++ ) {     if(!data[i].equals(data[i-1])){ //if new word found         words.add(data[i]);         //put words arraylist         nwords++;                   //increment index         occurrence[nwords]=0;       //initialize occurrence counter     }     occurrence[nwords]++;           //increment occurrence counter }  int max; for(int k=0; k<5; k++){  //loop find 5 times talked word   max = 0;               //index of talked word   for(int = 1; i<words.size(); i++) { //for every word     if(occurrence[i]>occurrence[max]) { //if more talked max       max = i;                          //than new talked     }   }   println("the talked keyword " + words.get(max) + " occuring " + occurence[max] + " times.");   occurence[max]=0; } 

every time find value higher occurence value, set occurrence counter 0 , reiterate again array, 5 times.


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -