2

Suppose I have a List<map> that looks like this:

‘Books’:[
    ‘B1’{
        ‘id’: ’1234’,
        ‘bookName’: ’book1’
    },
    ‘B2’{
        ‘id’: ’4567’,
        ‘bookName’: ’book2’
    },
    ‘B3’{
        ‘id’: ’1234’,
        ‘bookName’: ’book3’
    },
    ‘B4’{
        ‘id’: ’8912’,
        ‘bookName’: ’book4’
    },

    …
];

I’m trying to return the entire book without duplications in Id.

The expected result should be like this:

‘B1’{
        ‘id’: ’1234’,
        ‘bookName’: ’book1’
    },
‘B2’{
        ‘id’: ’4567’,
        ‘bookName’: ’book2’
    },
‘B4’{
        ‘id’: ’8912’,
        ‘bookName’: ’book4’
    },
1
  • You can't have a list looking like that. :) Can you clean that up to something I can assign to a variable? Commented Apr 12, 2021 at 1:30

2 Answers 2

4

I guessed what your input map was and made a solution based on this answer from Basic Coder.

final list = {
 'Books':[
    {
        'id':'1234',
        'bookName':'book1'
    },
    {
        'id':'4567',
        'bookName':'book2'
    },
    {
        'id': '1234',
        'bookName':'book3'
    },
    {
        'id': '8912',
        'bookName':'book4'
    },
]};

void main() {
  print('With duplicates $list');

  final ids = list['Books']!.map<String>((e) => e['id']!).toSet();
  list['Books']!.retainWhere((Map x) {
    return ids.remove(x['id']);
  });

  print('Without duplicates $list');
}

This code shows your input as the variable list, which seems to be what you were going for with your provided data. The code then obtains a list of each id of the book and removes duplicates by changing it to a Set. Then it only retains elements in the original list with those non-duplicate ids.

Remove the ! operators if you're not using null-safety.

Sign up to request clarification or add additional context in comments.

Comments

1

Here's one inspired by Christopher's answer:

void main() {
  var books = {
    'B1': {
      'id': '1234',
      'bookName': 'book1',
    },
    'B2': {
      'id': '4567',
      'bookName': 'book2',
    },
    'B3': {'id': '1234', 'bookName': 'book3'},
    'B4': {'id': '8912', 'bookName': 'book4'},
  };

  var seen = <String>{};

  var kept = books.entries.where((me) => seen.add(me.value['id'] ?? 'OTHER'));

  print(Map.fromEntries(kept));
}

I think it's a bit simpler, since it doesn't have to populate the Set first. I also learned that Set.add returns a bool to indicate the element didn't exist before. Nice.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.