Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get difference of lists flutter dart

Tags:

flutter

dart

I have two lists [1,2,3,4,5,6,7] and [3,5,6,7,9,10]. I want to get the difference of the first list and the second list.

The expected output would be [1,2,4] since those are the only elements in only list 1 and not list 2.

I'm using Flutter and Dart. I looked this up on the internet, I know it seems like a simple question but I couldn't find anything.

Should be irrelevant but I'm using publishing for iOS

I'd prefer an answer without just a foreach loop, im looking to see if there is a library for it.

like image 807
Sheshank S. Avatar asked Aug 23 '19 21:08

Sheshank S.


People also ask

How do you compare lists of objects in flutter?

here == operator the instance of the list not the content, where are equals method compare the data inside lists. List<String>list3=list1; print(list1==list3);

What is the difference between list and array in dart?

A very commonly used collection in programming is an array. Dart represents arrays in the form of List objects. A List is simply an ordered group of objects. The dart:core library provides the List class that enables creation and manipulation of lists.


1 Answers

Simple & Recommended Way to do this is

var a = [1,2,3,4,5];
var b = [1,2];
a.removeWhere((element) => b.contains(element));
print(a); //[3, 4, 5]
like image 124
Abhishek Ghimire Avatar answered Oct 18 '22 10:10

Abhishek Ghimire