So I'm working on an app in Flutter, and long story short: I have 2 'filter options' which create 2 seperate lists. Now what I want is to use those 2 Lists and find the items which are present in BOTH lists and add that item to a third List.
Example:
List<int> first_list = [1, 2, 3, 4, 5];
List<int> second_list = [1, 2, 8, 9];
Result: [1, 2]
I know I could potentially loop through 1 list and then check with 'contains()' if the item is present in the other list. But it could be that 1 (or both) lists are empty and then my third list will be empty as I simply will never loop to add a duplicate item to the third list
Something like this?
void main() {
List<int> first_list = [1, 2, 3, 4, 5];
List<int> second_list = [1, 2, 8, 9];
final shared = [...first_list.where(second_list.contains)];
print(shared); // [1, 2]
}
What you need is intersection between two collections. Most likely it makes sense not to use lists, but rather sets of items.
I would solve this like that:
void main() {
List<int> first_list = [1, 2, 3, 4, 5];
List<int> second_list = [1, 2, 8, 9];
final shared = first_list.toSet().intersection(second_list.toSet());
print(shared);
}
If you can work with sets, not with lists it would be even simpler:
void main() {
Set<int> first_list = {1, 2, 3, 4, 5};
Set<int> second_list = {1, 2, 8, 9};
final shared = first_list.intersection(second_list);
print(shared);
}
You can use intersection
List<int> firstList = [1, 2, 3, 4, 5];
List<int> secondList = [1, 2, 8, 9];
final firsToSet = firstList.toSet();
final secondToSet = secondList.toSet();
final res = firsToSet.intersection(secondToSet);
print(res);
Related
Store data in a 1-dimensional List and add() it to a 2-dimensional List.
After add(), .clear() the one-dimensional List.
However, since List refers to an address, clear() loses the address, so the 2D array becomes an empty List. Is there a way to keep the values in a 2D List even after clearing?
List<int> num = [1, 2, 3];
List<List<int> num2 = [];
num2.add(num);
num.clear();
You could use List.from() to perform shallow copy
List<int> num = [1, 2, 3];
List<List<int>> num2 = [];
num2.add(List.from(num));
print(num2); //[[1, 2, 3]]
num.clear();
print(num2); //[[1, 2, 3]]
use trick with toList() method
void main() {
List<int> num1 = [1, 2, 3];
List<List<int>> num2 = [[0,2]] ;
num2.add(num1.toList());
num1.clear();
print(num2); // [[0, 2], [1, 2, 3]]
print(num1); // []
}
You should add list as anonymous list
List<int> num = [1, 2, 3];
List<List<int>> num2 = [];
// add as anonymous list
num2.add(num.toList());
num.clear();
print(num2.length); // 1
print(num.length); // 0
for (var element in num2) {
print(element.length); // 3
}
I want to check if items in array existed and add new values from another array without overwriting element after reloading. I created such code:
//take from that array
List<int> list = [2, 3, 5];
// add to this array and check if this array already has the same element or not
List<int> newValueInt = [2, 6, 7];
list.forEach((item) {
if(!list.contains(item)){
newValueInt.add(item);
print(newValueInt);
}
});
and it shows me that print:
[2, 6, 7, 3]
[2, 6, 7, 3, 5]
List<int> list = [2, 3, 5];
// add to this array and check if this array already has the same element or not
List<int> newValueInt = [2, 6, 7];
List<int> temp = [];
for (var item in list) {
if(!newValueInt.contains(item)){
temp.add(item);
}
}
List<int> result = newValueInt + temp;
print(result); //---> [2, 6, 7, 3, 5]
if(!list.contains(item)){
to
if(! newValueInt.contains(item)){
How do we do math to each item in a list (add numbers from variable and multiply final value) and return another List?
Something like this:
var list = [1, 2, 3, 4, 5];
print(list); // Prints [1, 2, 3, 4, 5]
var list2 = list.map((i) => i * 2 + 5 /* math here*/).toList();
print(list2); // Prints [7, 9, 11, 13, 15]
I have an array in the flutter
final list = [1, 2, 3, 4, 5];
I want to loop through the array to calculate the sum of all the items in the array,how i can?
Try the following:
final list = [1, 2, 3, 4, 5];
final result = list.reduce((sum, element){
return sum + element;
});
This could do it
final sum = list.fold<int>(0, (previousValue, number) => previousValue + number);
I have two arrays of [PFObjects].
For example (simplified):
arr1: [PFObject] = [1, 2, 3, 4, 5, 6, 7, 8]
arr2: [PFObject] = [1, 2, 3, 4, 5]
What is the optimal way to compare arr1 with arr2 and only keep the duplicates (remove unique values).
So that arr1 looks like:
arr1 = [1, 2, 3, 4, 5]
let array = arr1.filter { arr2.contains($0) }
voilà !
First solution (Looping):
var arr1: [PFObject] = [1, 2, 3, 4, 5, 6, 7, 8]
var arr2: [PFObject] = [1, 2, 3, 4, 5]
var temp: [PFObject] = []
for element in arr1 {
if contains(arr2, element) {
temp.append(element)
}
}
arr1 = temp
You can loop over the first array, check if each element is contained in the array, if it is, you can add it to a temporary array. After looping over every element you can replace the value of the first array with your temporary array.
Second solution (Sets):
var arr1: [PFObject] = [1, 2, 3, 4, 5, 6, 7, 8]
var arr2: [PFObject] = [1, 2, 3, 4, 5]
let set1 = Set(arr1)
let set2 = Set(arr2)
var arr1= Array(set1.intersect(set2)) // [1, 2, 3, 4, 5]
What you do here is:
First you create sets from your arrays
Then you use the intersect method from sets to determine common elements
Finally you transform your set to an array before passing it back to arr1
Of course since you will be using sets, duplicate elements will be lost but I'm guessing that shouldn't be a problem in your case
Third solution (filter):
From the answer of Pham Hoan you can use filters to obtain a subset of arr1, the closure gives you the conditions, here it is that arr2 contains the value you are looking at.
let array = arr1.filter { arr2.contains($0) }
This is obviously the shorter solution in terms of code length.
I do not know which technique would be more efficient if you have very large arrays however.