Home > Enterprise >  Comparing two lists and finding indices of changes Dart
Comparing two lists and finding indices of changes Dart

Time:10-13

What is cleanest way to compare two list and find out different index, example:

list1 = ['M', 'A', 'N', 'H', 'T', 'U', 'A', 'N']
list2 = ['M', 'I', 'N', 'H', 'T', 'O', 'A', 'N']

How to get different index list [1, 5]

I was trying to loop each list, but it seem pretty awkward, is there any collection fuction to do this ?

CodePudding user response:

You could do this:

final result = IterableZip([list1, list2])
    .mapIndexed((index, element) => element[0] != element[1] ? index : null)
    .whereNotNull()
    .toList();

This needs

import 'package:collection/collection.dart';
  • Related