Home > Blockchain >  Flutter - How to find non-common elements in list with for loop?
Flutter - How to find non-common elements in list with for loop?

Time:12-08

List a = [3,5,7,8,14,32];
List b = [5,6,11,14,18,32,47];

I want to print [3,6,7,8,11,18,47]

CodePudding user response:

You can use toSet, toList and toShort methods to achieve you result. Like below.

List a = [3, 5, 7, 8, 14, 32];
List b = [5, 6, 11, 14, 18, 32, 47];

List c = a   b;
List d = c.toSet().toList();
d.sort();

Result: 3, 5, 6, 7, 8, 11, 14, 18, 32, 47

CodePudding user response:

List<int> getNonCommonList(List<int> a, List<int> b) {
  List<int> result = [];
  for (int e in a) {
    if (!b.contains(e)) {
      result.add(e);
    }
  }
  for (int e in b) {
    if (!a.contains(e)) {
      result.add(e);
    }
  }
  result.sort();
  return result;
}

List<int> a = [3,5,7,8,14,32];
List<int> b = [5,6,11,14,18,32,47];
getNonCommonList(a, b);
  • Related