Advertisement
Hiteshw11

Count the number of occurrences of each character in a string using a set or a map.

Nov 18th, 2024
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 0.51 KB | Source Code | 0 0
  1. //Count the number of occurrences of each character in a string using a set or a map.
  2.  
  3. void main()
  4. {
  5.   String a='banana';
  6.   print(uniqueCount(a));
  7. }
  8.  
  9. Map<String,int> uniqueCount(str)
  10. {
  11.   List<String> s1=str.split('');
  12.   Set<String> s2=Set.from(s1);
  13.   List<String> s11=s2.toList();
  14.   print(s1);
  15.   Map<String,int> abc={};
  16.  
  17.  
  18.   for(int i=0;i<s11.length;i++)
  19.   {
  20.     abc[s11[i]]=0;
  21.   }
  22.  
  23.   print(abc);
  24.  
  25.  
  26.   for(int j=0;j<s1.length;j++)
  27.   {
  28.     abc[s1[j]]=abc[s1[j]]!+1;
  29.   }
  30.  
  31.  
  32.   return abc;
  33. }
Tags: dart
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement