Zeffry Reynando
Zeffry Reynando

Reputation: 3899

Get the most value from Map

I want get the most value in List , then i calculate the value and return object.


class TestingModel {
  int id ;
  String name;
  int money;
  
  TestingModel({this.id,this.name,this.money});
}

List<TestingModel> myList = [
  TestingModel(
  id: 1,
  name: 'John',
  money: 200000
  ),
  TestingModel(
  id: 2,
  name: 'Doe',
  money: 400000
  ),
  TestingModel(
  id: 3,
  name: 'Mary',
  money: 800000
  ),
  TestingModel(
  id: 4,
  name: 'John',
  money: 300000
  ),
  TestingModel(
  id: 5,
  name: 'John',
  money: 500000
  ),
];

from above dummy example , my expectation result is The most value is 3 and have name John. For solved this case , i thinking firstly to group the list by name and get total value depending on name.

Extension GroupBy

extension Iterables<E> on Iterable<E> {
  Map<K, List<E>> groupBy<K>(K Function(E) keyFunction) => fold(
      <K, List<E>>{},
      (Map<K, List<E>> map, E element) =>
          map..putIfAbsent(keyFunction(element), () => <E>[]).add(element));
}

void main(){

final result = myList.groupBy((group)=>group.name);
print(result);
}

Result

{John: [Instance of 'TestingModel', Instance of 'TestingModel', Instance of 'TestingModel'], Doe: [Instance of 'TestingModel'], Mary: [Instance of 'TestingModel']}

But i stuck until here, i dont know how to find the most value from the map and fetch object depending on the name.

Expectation : Most Value found is 3 with name John

How can i do this ?

Upvotes: 0

Views: 93

Answers (2)

Alok
Alok

Reputation: 9008

My algorithm:

  • Loop over your list object
  • Have a map for adding the count for the names specifically
  • Get the largest data from map.entries.reduce
  • Print the value

Code

class TestingModel {
  int id;
  String name;
  int money;

  TestingModel({this.id, this.name, this.money});
}

void main() {
  // this to keep a track on the counter of the names
  Map<String, int> _nameCounter = {};
  
  List<TestingModel> myList = [
    TestingModel(id: 1, name: 'John', money: 200000),
    TestingModel(id: 2, name: 'Doe', money: 400000),
    TestingModel(id: 3, name: 'Mary', money: 800000),
    TestingModel(id: 4, name: 'John', money: 300000),
    TestingModel(id: 5, name: 'John', money: 500000),
  ];
  
  // iterating over your list of object
  myList.forEach((element){
    // if contain the name, add +1 
    if(_nameCounter.containsKey(element.name))
      _nameCounter[element.name] += 1;
    else
      _nameCounter[element.name] = 1;
  });
  
  final maxData = _nameCounter.entries.reduce((a, b) => a.value > b.value ? a : b);
  print('${maxData.key}: ${maxData.value}');
}

Output

Jhon: 3

Upvotes: 2

julemand101
julemand101

Reputation: 31259

I would do something like this:

class TestingModel {
  int id;
  String name;
  int money;

  TestingModel({this.id, this.name, this.money});
}

List<TestingModel> myList = [
  TestingModel(id: 1, name: 'John', money: 200000),
  TestingModel(id: 2, name: 'Doe', money: 400000),
  TestingModel(id: 3, name: 'Mary', money: 800000),
  TestingModel(id: 4, name: 'John', money: 300000),
  TestingModel(id: 5, name: 'John', money: 500000),
];

void main() {
  final map = myList.fold(
      <String, int>{},
      (Map<String, int> fold, element) =>
          fold..update(element.name, (value) => value + 1, ifAbsent: () => 1));
  print(map); // {John: 3, Doe: 1, Mary: 1}

  final largest = map.entries.reduce((a, b) => a.value > b.value ? a : b);
  print('Most Value found is ${largest.value} with name ${largest.key}');
  // Most Value found is 3 with name John
}

Upvotes: 3

Related Questions