Finding the index of the smallest element in an array (Java)
Asked Answered
H

4

6

I am trying to write up a block of code that takes an array of integers as an argument and returns the index of the smallest element in the array. Also, the function should return -1 if the list is an empty list.

Test for the block of code.

So far I have got,

public static int indexOfSmallest(int[] array){
    int index = 0;
    int min = array[index];

    for (int i = 1; i < array.length; i++){
        if (array[i] <= min){
        min = array[i];
        index = i;
        }
    }
        return index;
}

But, I'm getting this error and unsure what I need to fix.

enter image description here

Any help would be much appreciated. Thank you.

Hyland answered 5/8, 2018 at 9:27 Comment(0)
E
5

The error is self explanatory. You fail to handle the case of empty input array.

public static int indexOfSmallest(int[] array){

    // add this
    if (array.length == 0)
        return -1;

    int index = 0;
    int min = array[index];

    for (int i = 1; i < array.length; i++){
        if (array[i] <= min){
        min = array[i];
        index = i;
        }
    }
    return index;
}

If the smallest element appears multiple times, and you want to return the index of its first occurrence, change your condition to:

if (array[i] < min) 
Eidolon answered 5/8, 2018 at 9:30 Comment(5)
That helped a lot, but now I'm getting another error when I run this test. int[] numbers = new int[]{100, 4, 16, 15, 4}; System.out.print(indexOfSmallest(numbers));Hyland
@Hyland you have two indices that contain the smallest number. Are you required to return the first of them or the last of them. If the first, change if (array[i] <= min) to if (array[i] < min)Eidolon
It was the first one. Removing the '=' sign solved it. Thank you for your help.Hyland
I'd use an if/else condition to avoid having multiple return statements.Bacteriology
@Bacteriology I like this approach TBH, as multiple returns also means less code needs to be executed which is better for memory and performance.Dupondius
D
0

If your numbers array is empty, and index is declared as 0, then min = array[index] will cause an error as you're trying to access the nonexistent first element of an empty list.

You should insert an if(before the for loop) that checks if the numbers array is empty.

Dupondius answered 5/8, 2018 at 9:36 Comment(0)
I
0

You are getting IndexOutOfBoundsException because you are trying to retrieve a value from an empty array in the line:

int min = array[index];

Just check if array is empty before this line using:

 if(array.length < 1) return -1;
Interrelate answered 5/8, 2018 at 10:13 Comment(0)
H
0
public static int indexOfSmallest(int[] arr) {
    int imin = 0;
    for (int i = 1; i < arr.length; i++) {
        if (arr[i] < arr[imin]) {
            imin = i;
        }
    }
    return imin;
}

or

int min = Arrays.stream(arr).min().orElseThrow();
int imin = Arrays.stream(arr).boxed().collect(Collectors.toList()).indexOf(min);
Hued answered 17/10, 2020 at 19:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.