1

i have the following list with duplicate values and i need to check if the values is duplicated print it else skip it

List lst = ["AA","BB","BBB","AA"];

what should printed is :

AA

Thanks ;

1
  • 1
    Be sure to include a brief overview of what you have tried and/or what research you have done in your question's post. Commented Jan 25, 2020 at 14:31

3 Answers 3

3

Would this work for you?

List list = ["AA", "BB", "BBB", "AA"];
List distinctList = list.toSet().toList();

void main() {

  for (int i = 0; i < distinctList.length; i++) {
    list.remove(distinctList[i]);
  }

  //// or you could use a "for in" like this:
  // for (var item in distinctList) {
  //   list.remove(item);
  // }

  print(list.toSet().toList());

}


Or using forEach() on the Set

List list = ['AA', 'BB', 'BBB', 'AA'];

void main() {
  list.toSet().forEach((item) => {list.remove(item)});

  print(list.toSet().toList());
}
Sign up to request clarification or add additional context in comments.

Comments

1
lst.where((e) => lst.where((element) => element == e).length > 1).toSet().toList();

One Liner

Comments

0
 List lst = ["AA","BB","BBB","AA"];
  
  int duplicateDetectedCount = -1;
  
  String duplicateItem = "";
  
  for(String item in lst){  
    for(String innerItem in lst){
      if(item == innerItem){
        duplicateDetectedCount++;
        if(duplicateDetectedCount == 1){
          duplicateItem = innerItem;
        }
      }
    }
  }
  
  print(duplicateItem); // prints AA

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.