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

  • Comp Sci
  • Thread starter bc030400412
  • Start date
  • Tags
    Array Java
In summary, the function described accepts an array of integers and returns the second largest integer in the array. If there is no second largest integer, it returns -1. The code provided uses a loop to iterate through the array and compare each element to find the second largest number. If there is no second largest number, it returns -1.
  • #1
bc030400412
9
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
  • #2
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.
 
  • #3
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.


}
 

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

What is a simple array in Java?

A simple array in Java is a data structure that stores a fixed-size collection of elements of the same data type. It can hold a sequence of values and allows for easy access and manipulation of those values.

How do you declare a simple array in Java?

To declare a simple array in Java, you need to specify the data type of the elements it will hold, followed by square brackets and the name of the array. For example, to declare an array of integers with a size of 5, you would use the following syntax: int[] myArray = new int[5];

How do you assign values to a simple array in Java?

You can assign values to a simple array in Java using a for loop or by directly assigning values to each index. For example, to assign the values 1, 2, 3, 4, 5 to the array declared in the previous question, you could use the following code:

for (int i = 0; i < myArray.length; i++) {
    myArray[i] = i + 1;
}

How do you access elements in a simple array in Java?

To access elements in a simple array in Java, you use the index of the element you want to access. Array indexes start at 0, so to access the first element in the array, you would use myArray[0]. You can also use a loop to iterate through the array and access each element individually.

What is the length of a simple array in Java?

The length of a simple array in Java is the number of elements it can hold. This is specified when the array is declared and cannot be changed afterwards. You can use the length property to get the length of an array, for example: myArray.length.

Similar threads

Replies
21
Views
2K
Replies
3
Views
985
Replies
3
Views
1K
Replies
3
Views
1K
Replies
5
Views
2K
Replies
2
Views
1K
Replies
5
Views
2K
Replies
1
Views
2K
Replies
1
Views
1K
Back
Top