Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find duplicate elements length in array flutter?

Tags:

flutter

I want to implement add to checkout in which number of items added is displayed. Plus button adds elements in list and minus removes elements from list. Goal is just to display particular items added and its quantity. I have added items in list want to count length of duplicate items. How we can do that in flutter?

like image 884
vbakale Avatar asked Nov 01 '25 03:11

vbakale


1 Answers

here is your solution. [Null Safe]

void main() {
  List<int> items = [1, 1, 1, 2, 3, 4, 5, 5];

  Map<int, int> count = {};
  items.forEach((i) => count[i] = (count[i] ?? 0) + 1);

  print(count.toString()); // {1: 3, 2: 1, 3: 1, 4: 1, 5: 2}
}
like image 164
towhid Avatar answered Nov 03 '25 20:11

towhid