Open harshraj8843 opened 10 months ago
👋🏻 Hey @harshraj8843
💖 Thanks for opening this issue 💖
A team member should be by to give feedback soon.
!assign
!assign
Hey @DongJu-Na, this issue is already assigned to @zackbmz! cc/ @codinasion/codinasion
import java.util.Arrays;
public class Main { public static void main(String[] args) { int[] array = {5, 8, 1, 6, 3, 9, 2, 7, 4};
findLargestTwo(array);
}
public static void findLargestTwo(int[] array) {
if (array.length < 2) {
System.out.println("Array should have at least two elements.");
return;
}
int firstLargest = Integer.MIN_VALUE;
int secondLargest = Integer.MIN_VALUE;
for (int num : array) {
if (num > firstLargest) {
secondLargest = firstLargest;
firstLargest = num;
} else if (num > secondLargest && num != firstLargest) {
secondLargest = num;
}
}
if (secondLargest == Integer.MIN_VALUE) {
System.out.println("There are no distinct largest elements in the array.");
} else {
System.out.println("The largest two elements in the array are: " + firstLargest + " and " + secondLargest);
}
}
}
!assign
Hey @abhinav-selvaraj, this issue is already assigned to @zackbmz! cc/ @codinasion/codinasion
!assign
Hey @GuyBernstein, this issue is already assigned to @zackbmz! cc/ @codinasion/codinasion
!assign
Hey @PietervHa, this issue is already assigned to @zackbmz! cc/ @codinasion/codinasion
Description
Write a Java program to find the largest two elements in an array
How to contribute
- Comment `!assign` to assign this issue to yourself - Fork this repository - Create a new branch - Save the solution in `program/program/find-the-largest-two-elements-in-an-array/FindTheLargestTwoElementsInAnArray.java` - Commit the changes - Create a pull request