by @kodeazy
flutter get the largest number in array
-
We have an array of numbers as below and we have to find the largest number in array.
var items = [1, 2, 3, 4];
- To get the largest number in array initialize first element as
largest
. - Now iterate through all the elements in the array and compare the
largest
element with each element in the array. -
If the element in the array is greater than the element in the variable
largest
replace thelargest
variable value with the array element value.import 'package:flutter/material.dart'; void main() { var items = [1, 2, 3, 4]; var largest = items[0]; for (var item in items) { if (item > largest) largest = item; } print(largest); }