Home > Blockchain >  check if items in array existed and add new values from another array without overwriting?
check if items in array existed and add new values from another array without overwriting?

Time:11-19

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]

CodePudding user response:

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]

CodePudding user response:

if(!list.contains(item)){

to

if(! newValueInt.contains(item)){
  • Related