Nitneuq
Nitneuq

Reputation: 5012

How to count identical date in List String with flutter?

I try to count the number of the same date from a list of String.

Here is an example from MyList_date_input:

    MyList_date_input=[2020-09-08 11:16, 2020-09-08 08:10, 2020-09-09 21:23, 2020-09-09 21:59, 2020-09-10 07:46]


      for (var i = 0; i < MyList_date_input.length; i++) {

      ...
      ???
      ...
      stringList_date_output.add("$date,mykey:$count_date"); // I search to save 
 
     }

Here is my expected output from stringList_date_output.

Thank you!

List_date_output=[2020-09-08,mykey:2,2020-09-09,mykey:22020-09-10,mykey:1]

Upvotes: 0

Views: 372

Answers (3)

biniyam112
biniyam112

Reputation: 1182

Here you go!

List<Map<String, int>> similarDaysCounter(List<String> days) {
  var daystemp = days;

  List<Map<String, int>> daysCount = [];
  while (days.length != 0) {
    for (var i = 0; i < days.length; i++) {
      int counter = 0;
      for (var j = 0; j < daystemp.length; j++) {
        if (days[i] == daystemp[j]) {
          counter++;
        }
      }
      daysCount.add({days[i]: counter});
      days.removeWhere((element) => (element == days[i]));
      print(daysCount);
      // print(days);
      // print(daystemp);
    }
  }
  return daysCount;
}

Hope this is helpful!

Upvotes: 0

Rahul sharma
Rahul sharma

Reputation: 1574

It is Simple just create HasMap for each date and its count. Like this

  HashMap map=HashMap<String,int>();
  list.forEach((element) {  // List which contain dates
      int temp=map[element];
      if(temp!=null) // if is already registered date  
      {
        map[element]=temp++; // Then it will increment the count
      }
      else // If it is new date
       {
        map[element]=1; //Then it will add the count
    }
 });

Note Remove time from the date before doing this.

Upvotes: 0

Stefano Amorelli
Stefano Amorelli

Reputation: 4854

You're probably looking for Map:

Map<String, int> countDateDupes(List<String> list) {
  Map<String, int> returnMap = {};
  for (var i = 0; i < list.length; i++) {
    DateTime parsedDate = DateTime.parse(list[i]);
    String date = "${parsedDate.year}-${parsedDate.month}-${parsedDate.day}";
    if (returnMap.containsKey(date)) {
      returnMap[date]++;
    } else {
      returnMap[date] = 1;
    }
  }
  return returnMap;
}

Follows a full example:

main() {
  List<String> myListDateInput = [
    '2020-09-08 11:16',
    '2020-09-08 08:10',
    '2020-09-09 21:23',
    '2020-09-09 21:59',
    '2020-09-10 07:46',
  ];
  print(countDateDupes(myListDateInput));
  // outputs: {2020-9-8: 2, 2020-9-9: 2, 2020-9-10: 1}
}

Map<String, int> countDateDupes(List<String> list) {
  Map<String, int> returnMap = {};
  for (var i = 0; i < list.length; i++) {
    DateTime parsedDate = DateTime.parse(list[i]);
    String date = "${parsedDate.year}-${parsedDate.month}-${parsedDate.day}";
    if (returnMap.containsKey(date)) {
      returnMap[date]++;
    } else {
      returnMap[date] = 1;
    }
  }
  return returnMap;
}

Upvotes: 1

Related Questions