Advertisement
Hiteshw11

Finding Max and Min Elements In A List In Dart

Nov 17th, 2024
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 0.67 KB | Source Code | 0 0
  1. /*Write A function that takes a list of integers and finds the maximum and minimum elements in the list without using built-in max and min functions.*/
  2.  
  3. void main() {
  4.   List<int> nums = [3, 1, 7, 2, 5];
  5.   print("The greatest number in the list is ${great(nums)}");
  6.   print("The smallest number in the list is ${minimum(nums)}");
  7. }
  8.  
  9. int great(List<int> abc) {
  10.   int big = abc[0];
  11.   for (int i = 0; i < abc.length; i++) {
  12.     if (abc[i] > big) {
  13.       big = abc[i];
  14.     }
  15.   }
  16.   return big;
  17. }
  18.  
  19. int minimum(List<int> def) {
  20.   int small = def[0];
  21.   for (int j = 0; j < def.length; j++) {
  22.     if (def[j] < small) {
  23.       small = def[j];
  24.     }
  25.   }
  26.   return small;
  27. }
  28.  
Tags: dart
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement