Find the Second Largest Integer in a Java Array | Simple Function Example

  • Context: Comp Sci 
  • Thread starter Thread starter bc030400412
  • Start date Start date
  • Tags Tags
    Array Java
Click For Summary
SUMMARY

The discussion focuses on creating a Java function to find the second largest integer in an array. The provided function signature is int f(int[] a), and the implementation demonstrates how to iterate through the array to identify the largest and second largest integers. Key examples include arrays such as {1, 2, 3, 4} returning 3 and {1, 1} returning -1 when no second largest integer exists. The code effectively handles edge cases, including empty arrays and arrays with duplicate values.

PREREQUISITES
  • Java programming fundamentals
  • Understanding of arrays in Java
  • Basic knowledge of control structures (loops and conditionals)
  • Familiarity with handling edge cases in algorithms
NEXT STEPS
  • Implement error handling for invalid input arrays in Java
  • Explore Java Streams for functional programming approaches to find the second largest integer
  • Learn about time complexity analysis for the implemented algorithm
  • Investigate alternative methods for finding the second largest integer, such as sorting
USEFUL FOR

Java developers, computer science students, and anyone interested in algorithm design and array manipulation in Java.

bc030400412
Messages
9
Reaction score
0
Q1. Write a function that accepts an array of integers and returns the second largest integer in the array. Return -1 if there is no second largest.

The signature of the function is

public class ID
{
public static void main(String[] args){ }
int f(int[ ] a) { }

}

Examples:

if the input array is {1, 2, 3, 4} then return 3
if the input array is {{4, 1, 2, 3}} then return 3
if the input array is {1, 1, 2, 2} then return 1
if the input array is {1, 1} then return -1
if the input array is {1} then return -1
if the input array is {} then return -1
 
Last edited:
Physics news on Phys.org
Ok, you have the signature, what about the actual code?

The examples you've given doesn't demonstrate you've attempted the program. Anyone can pick the second largest number in a set. Please show us some code.
 
public static void main(String[] args) {
// TODO code application logic here

int secondlargest= -1;
int largest = -1;

int number[] ={4, 1, 2, 3};

for (int i=0;i < number.length;i++)
{

if(largest < number )
{
secondlargest = largest;
largest = number;
}

if(secondlargest < number && largest != number )
secondlargest = number;

}

System.out.println(secondlargest);
//System.out.println(largest);
// System.out.println(objhwa.data); // Display the string.


}
 

Similar threads

  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K