Skip to content

Instantly share code, notes, and snippets.

@ervinod
Created January 27, 2025 18:49
Show Gist options
  • Select an option

  • Save ervinod/f75336aa2c2c7c5396c9a7a8499f049e to your computer and use it in GitHub Desktop.

Select an option

Save ervinod/f75336aa2c2c7c5396c9a7a8499f049e to your computer and use it in GitHub Desktop.
Find the second largest number of the array list
void main() {
List<int> nos = [1, 2, 23, 5, 12, 8, 7];
int largest1 = nos[0];
int largest2 = nos[0];
void solution1() {
for (var i = 0; i < nos.length; i++) {
int current = nos[i];
if (current > largest1) {
largest2 = largest1;
largest1 = current;
} else {
if (current > largest2) {
largest2 = current;
}
}
}
}
void solution2() {
for (var i = 0; i < nos.length; i++) {
for (int j = i + 1; j < nos.length; j++) {
if (nos[i] < nos[j]) {
int temp = nos[i];
nos[i] = nos[j];
nos[j] = temp;
}
}
}
largest1 = nos[0];
largest2 = nos[1];
}
solution1();
//solution2();
print("Second Largest No: ${largest2}");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment