I have the list of elements in flutter like this in dart:
我在飞镖中有一系列元素,就像这样:
[3, 1, 0, 1, 3, 1]
[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.
我想这样做:如果这个列表的‘1’元素重复三次,那么就做点什么。如你所见,数字‘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.");
}
}
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;
}
}
}
}
void main() {
List numbers = [3, 1, 0, 1, 3, 1];
Void main(){列表编号=[3,1,0,1,3,1];
// Create a map to count occurrences
Map<int, int> counts = {};
//创建一个map来统计事件Map
count={};
for (var number in numbers) {
counts[number] = (counts[number] ?? 0) + 1;
}
bool hasRepeatedThreeTimes = counts.values.any((value) => value >= 3);
print(hasRepeatedThreeTimes);
}
}
void main() {
List<int> numbers = [3, 1, 0, 1, 3, 1];
int targetElement = 1;
int repetitionCount = 3;
print(isElementRepeated(numbers,3,2));
}
bool isElementRepeated(List<int> numbers, int targetElement, int repetitionCount) {
int count = numbers.fold(0, (int acc, int element) {
return element == targetElement ? acc + 1 : acc;
});
return count == repetitionCount;
}
更多回答
我是一名优秀的程序员,十分优秀!