Try to search your question here, if you can't find : Ask Any Question Now ?

How to count items' occurence in a List

HomeCategory: stackoverflowHow to count items' occurence in a List
Avatarsourav asked 5 months ago

I am new to Dart. Currently I have a List of duplicate items, and I would like to count the occurence of them and store it in a Map.

var elements = ["a", "b", "c", "d", "e", "a", "b", "c", "f", "g", "h", "h", "h", "e", "a"];

I want to have a result like:

  "a": 3,
  "b": 2,
  "c": 2,
  "d": 2,
  "e": 2,
  "f": 1,
  "g": 1,
  "h": 3

I did some research and found a JavaScript solution, but I don’t know how to translate it to Dart.

var counts = {};
your_array.forEach(function(x) { counts[x] = (counts[x] || 0)+1; });
1 Answers
Best Answer
Avatarnaveen answered 5 months ago
Your Answer

5 + 17 =

Popular Tags

WP Facebook Auto Publish Powered By :