Skip to content
This repository has been archived by the owner on Jul 10, 2024. It is now read-only.

Write a Java program to find the largest two elements in an array #5042

Open
harshraj8843 opened this issue Jan 8, 2024 · 11 comments
Open
Assignees
Labels
good first issue Good for newcomers Java java related program triage Waiting for review

Comments

@harshraj8843
Copy link
Contributor

Description

Write a Java program to find the largest two elements in an array

Input: arr[] = {12, 13, 1, 10, 34, 1}
Output: The largest two elements are 34 and 13

Input: arr[] = {10, 5, 10}
Output: The largest two elements are 10 and 5

Input: arr[] = {10, 10, 10}
Output: The largest two elements are 10 and 10
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
@harshraj8843 harshraj8843 added good first issue Good for newcomers Java java related program labels Jan 8, 2024
@codinasion-bot
Copy link

codinasion-bot bot commented Jan 8, 2024

👋🏻 Hey @harshraj8843

💖 Thanks for opening this issue 💖

A team member should be by to give feedback soon.

@zackbmz
Copy link
Contributor

zackbmz commented Jan 13, 2024

!assign

@DongJu-Na
Copy link

!assign

@codinasion-bot
Copy link

codinasion-bot bot commented Feb 4, 2024

Hey @DongJu-Na, this issue is already assigned to @zackbmz! cc/ @codinasion/codinasion

@savarsri
Copy link

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);
    }
}

}

@abhinav-selvaraj
Copy link

!assign

@codinasion-bot
Copy link

Hey @abhinav-selvaraj, this issue is already assigned to @zackbmz! cc/ @codinasion/codinasion

@GuyBernstein
Copy link

!assign

@codinasion-bot
Copy link

Hey @GuyBernstein, this issue is already assigned to @zackbmz! cc/ @codinasion/codinasion

@PietervHa
Copy link
Contributor

!assign

@codinasion-bot
Copy link

Hey @PietervHa, this issue is already assigned to @zackbmz! cc/ @codinasion/codinasion

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
good first issue Good for newcomers Java java related program triage Waiting for review
Projects
None yet
Development

No branches or pull requests

7 participants