I want to know if there is a way to improve my code. It finds the two highest values in an array, and these numbers need to be distinct.
I don't want to sort the values; I just want to find them.
My idea is to create a new array if the length is even and compare the pair of values to find the minimum and maximum values.
So, later, I find the second highest value.
package doze;
public class Elementos {
public int maxValue(int array[], int arrayLength) {
arrayLength = array.length;
if ((arrayLength % 2) > 0) {
arrayLength++;
int aux[] = array;
array = new int[arrayLength];
for (int i = 0; i < aux.length; i++) {
array[i] = aux[i];
}
array[(arrayLength - 1)] = aux[(arrayLength - 2)];
}
int maxValue = array[0];
int minValue = array[0];
int i = 0;
while (i != arrayLength) {
if (array[i] > array[i + 1]) {
if (array[i] > maxValue) {
maxValue = array[i];
}
if (array[i + 1] < minValue) {
minValue = array[i + 1];
}
} else {
if (array[i + 1] > maxValue) {
maxValue = array[i + 1];
}
if (array[i] < minValue) {
minValue = array[i];
}
}
i += 2;
}
int secondMaxValue = minValue;
i = 0;
while (i != arrayLength) {
if ((array[i] > minValue) && (array[i] < maxValue)) {
minValue = array[i];
secondMaxValue = minValue;
}
i += 1;
}
return maxValue;
}
}