Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to count items' occurence in a List

Tags:

list

dart

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; });
like image 625
Keyser Soze Avatar asked Apr 08 '19 18:04

Keyser Soze


People also ask

How do you count occurrences of elements in a list?

Using the count() Function The "standard" way (no external libraries) to get the count of word occurrences in a list is by using the list object's count() function. The count() method is a built-in function that takes an element as its only argument and returns the number of times that element appears in the list.

How do you count occurrences in a list in Python?

The easiest way to count the number of occurrences in a Python list of a given item is to use the Python . count() method. The method is applied to a given list and takes a single argument. The argument passed into the method is counted and the number of occurrences of that item in the list is returned.

How do you count the number of repeated items in a list Python?

Operator. countOf() is used for counting the number of occurrences of b in a. It counts the number of occurrences of value. It returns the Count of a number of occurrences of value.

How do you count items in a for loop list?

Using For Loop for loop is used to iterate over a sequence of values. To get the number of elements in the list, you'll iterate over the list and increment the counter variable during each iteration. Once the iteration is over, you'll return the count variable, which has the total number of elements in the list.


2 Answers

Play around with this:

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

  elements.forEach((element) {
    if(!map.containsKey(element)) {
      map[element] = 1;
    } else {
      map[element] +=1;
    }
  });

  print(map);

What this does is:

  • loops through list elements
  • if your map does not have list element set as a key, then creates that element with a value of 1
  • else, if element already exists, then adds 1 to the existing key value

Or if you like syntactic sugar and one liners try this one:

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

  elements.forEach((x) => map[x] = !map.containsKey(x) ? (1) : (map[x] + 1));

  print(map);

There are many ways to achieve this in all programming languages!

like image 187
Matija Avatar answered Oct 23 '22 21:10

Matija


The shorter way to count items' occurrence in a List

List of items. Count items equal 1.

List<int> lst = [0,1,1,1,0,8,8,9,1,0];
int res = lst.map((element) => element == 1 ? 1 : 0).reduce((value, element) => value + element);

List of objects. Count objects, which property age equals 1.

class Person {
   int age;
   
   Person(this.age);
}

List<Person> lst2 = [Person(1), Person(0), Person(1), Person(0)];
int res2 = lst2.map((element) => element.age == 1 ? 1 : 0).reduce((value, element) => value + element);
like image 41
awaik Avatar answered Oct 23 '22 19:10

awaik