skip to Main Content

I have the list of elements in flutter like this in dart:

[3, 1, 0, 1, 3, 1]

I want to this:
if ‘1’ element of this list is repeated three times then do something.
as you see ‘1’ number has been repeated three times.

2

Answers


  1. Try this

    void main() {
      List<int> myList = [3, 1, 0, 1, 3, 1];
    
      // Count the occurrences of '1' in the list
      int countOfOnes = myList.where((element) => element == 1).length;
    
      // Check if '1' is repeated three times
      if (countOfOnes == 3) {
        // Do something
        print("The element '1' is repeated three times.");
      } else {
        // Do something else
        print("The element '1' is not repeated three times.");
      }
    }
    
    Login or Signup to reply.
  2. void main() {
      List<int> numbers = [3, 1, 0, 1, 3, 1];
      int count = 0;
    
      for (int number in numbers) {
        if (number == 1) {
          count++;
          if (count == 3) {
            print("The number '1' is repeated three times.");
            break;
          }
        }
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search