-2

This is what I got for the highest value, that seems to be working but im struggling to get the second highest value in a similair way.

public Show GetHighestScore() {      
    Show highest = shows.get(0);
    for (Show show : shows) {
        if (show.getScore() > highest.getScore()) {
            highest = show;
        }
    }
    return highest;
}
Torando1
  • 25
  • 3
  • Could you please put the code of Show class so we can propose better solution – JHDev Mar 21 '21 at 12:57
  • If your java version is 8 or higher, you can use stream to sort the ArrayList after that you can get the Kth smallest element in your case second smallest element : public Show kthSmallestUsingStream(List shows, int k) { if (shows.size() < k || k <= 0) { throw new IndexOutOfBoundsException(k); } List sortedShows = shows.stream() .parallel() .sorted((sh01, sh02) -> sh01.getScore() - sh02.getScore()) .collect(Collectors.toList()); return sortedShows.get(k - 1); } – JHDev Mar 21 '21 at 13:53

3 Answers3

0
public float secondHighest(ArrayList<Float> array){
  float highest = 0;
  for(float i : array){
      if(i > highest) highest = i;
  }
  float secondHighest = 0;
  for(float i : array){
      if(i > secondHighest && i < highest) secondHighest = i;
  }
  return secondHighest;
}
Luis Ka
  • 95
  • 7
  • This code does not compile with 2 compilation errors on assignments _possible lossy conversion from float to int_ Also it makes assumptions about the values in the input `array` and will provide incorrect result in edge cases when there are less than 2 elements. – Nowhere Man Mar 21 '21 at 13:03
  • Of course. This is just the most minimalistic principle. With just a little bit of Java knowledge anyone can extend the code to their needs. If you don't know how to adapt the function to your code I can help you. – Luis Ka Mar 21 '21 at 13:36
0

This is the typical problem of finding the K'th smallest/largest element in an array. I suggest you visit this link. But in your case, willing to get the second highest number, you can simply make two passes on the array with a similar idea to selection sort or the way Luis Ka has provided.

fmatt
  • 464
  • 1
  • 5
  • 15
0

This should work in one pass and return appropriate null if no second highest value is available in the data and making no assumptions about the values in the input data:

static <T extends Comparable> T find2ndMax(List<T> data) {
    if (null == data || data.size() < 2) {
        return null;
    }
    T max1 = data.get(0).compareTo(data.get(1)) > 0 ? data.get(0) : data.get(1);
    T max2 = data.get(0).compareTo(data.get(1)) < 0 ? data.get(0) : data.get(1);
    
    for (int i = 2, n = data.size(); i < n; i++) {
        T x = data.get(i);
        if (x.compareTo(max1) > 0) {
            max2 = max1;
            max1 = x;
        } else if (x.compareTo(max2) > 0) {
            max2 = x;
        }
    }
    return max2;
}

Tests (comparing to the fixed method of the accepted answer):

List<List<Float>> tests = Arrays.asList(
    null,
    Collections.emptyList(),
    Arrays.asList(-1f),
    Arrays.asList(-1f, -2f),
    Arrays.asList(-1f, 1f),
    Arrays.asList(-1f, -2f, 3f),
    Arrays.asList(-1f, 4f, -2f, 2f)
);
for (List<Float> t : tests) {
    System.out.print("2nd max in " + t + ": ");
    System.out.print("find2Max() = " + find2ndMax(t) + "; secondHighest() = " );
    try {
        System.out.print(secondHighest(t)); 
    } catch(Exception ex) {
        System.out.print("FAIL: " + ex.getMessage());
    }
    System.out.println();
}

Output

2nd max in null: find2Max() = null; secondHighest() = FAIL: null
2nd max in []: find2Max() = null; secondHighest() = 0.0
2nd max in [-1.0]: find2Max() = null; secondHighest() = 0.0
2nd max in [-1.0, -2.0]: find2Max() = -2.0; secondHighest() = 0.0
2nd max in [-1.0, 1.0]: find2Max() = -1.0; secondHighest() = 0.0
2nd max in [-1.0, -2.0, 3.0]: find2Max() = -1.0; secondHighest() = 0.0
2nd max in [-1.0, 4.0, -2.0, 2.0]: find2Max() = 2.0; secondHighest() = 2.0
Nowhere Man
  • 19,170
  • 9
  • 17
  • 42