Nitneuq
Nitneuq

Reputation: 5012

How to remove all same data from a list<String> exept the last ? with flutter

I search to remove all same data from a list exept the last same

I have an input list like that :

[2020-11-04,note0:blabla, ... data_random ... 2020-11-04,note0:srgg, ... data_random ... ,2020-11-15,note0:test, ... data_random ... , 2020-11-15,note0:test1, ... data_random ... , 2020-11-15,note0:test2, ... data_random ... , 2020-11-15,note0:test3]

I search to keep only the last 2020-11-15,note0:test3

and remove 2020-11-04,note0:blabla, 2020-11-04,note0:srgg, 2020-11-15,note0:test, 2020-11-15,note0:test1, 2020-11-15,note0:test2

thank you

Upvotes: 0

Views: 141

Answers (1)

Sajad Abdollahi
Sajad Abdollahi

Reputation: 1741

since they are common in "2020-11-15,note0:" you can use a reverse for loop like this and check if they contain this: "2020-11-15,note0:"

List keepTheLast() {
List input = [
  "2020-11-15,note0:test",
  "... data_random ... ",
  "2020-11-15,note0:test1",
  "... data_random ...",
  "2020-11-15,note0:test2",
  "... data_random ... ",
  "2020-11-15,note0:test3"
];
bool found = false;
for (int i = input.length - 1; i >= 0; i--)
  if (input[i].contains("2020-11-15,note0:"))
    found ? input.removeAt(i) : found = true;
print(input.toString());
return input;}

and the res would be:

 [... data_random ... , ... data_random ..., ... data_random ... , 2020-11-15,note0:test3]

Upvotes: 1

Related Questions