Advertisement
Hiteshw11

Count Frequency Of Each Word In A List

Nov 22nd, 2024
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 0.42 KB | Source Code | 0 0
  1. void main() {
  2.   List<String> words =['apple','banana','apple','orange','banana','apple'];
  3.   print(countocc(words));
  4. }
  5.  
  6. Map<String,int> countocc(words)
  7. {
  8. Map<String,int> counter={};
  9. Set <String> abc =Set.from(words);
  10. List<String> w2=abc.toList();
  11.  
  12.  
  13.  
  14. for(int i=0;i<w2.length;i++)
  15. {
  16. counter[w2[i]]=0;
  17. }
  18.  
  19. for(int i=0;i<words.length;i++)
  20. {
  21.    counter[words[i]]=counter[words[i]]!+1;
  22. }
  23.  
  24. return counter;
  25.  
  26.  
  27.  
  28. }
Tags: dart
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement