cmsp
cmsp

Reputation: 15

how to return a position from an array in Java

I've been given an array. I need to get the minimum value from it, and then return the value's position in the array. I'm newer to Java, and I only really have experience in Python. Here is my code so far.

public static int minPosition (int[] list) {
    int currMin = list[0];
    int index = list.length-1;
    int currPos = 0;

    while (index >= 0){
        if (currMin > list[index])
            currMin = list[index];
        if (currMin > list[index])
            currPos = index;

        index--;

    }
    return currPos;
}

These are my arrays that get called automatically.

minPosition(new int[] { -7 }));
minPosition(new int[] { 1, -4, -7, 7, 8, 11 }));
minPosition(new int[] { -13, -4, -7, 7, 8, 11 }));
minPosition(new int[] { 1, -4, -7, 7, 8, 11, -9 }));

Thank you in advance for the advice.

Upvotes: 0

Views: 1491

Answers (5)

sashwat
sashwat

Reputation: 647

Assuming a not null array, you can first sort the list and then return the first from sorted list. This will not be as efficient as some examples above but the code will be the shorter.

public static int minPosition (int[] list) {
        Arrays.sort(list);
        return list[0];
}

Upvotes: 0

Joseph K. Strauss
Joseph K. Strauss

Reputation: 4913

This solution does not necessitate keeping track of the current minimum. This makes the code easier to follow, and leaves less room for errors. It also starts searching from the beginning, which your original code is not doing. (I am not sure why that is so, since you would presumably want the first index that has a minimum value, not the last.)

public static int minPosition (int[] list) {
  if( list == null || list.length = 0){
    return -1;
  }

  int minPos = 0;

  for(int i = 0; i < list.length; i++){
    if(list[i] < list[minPos]){
      minPos = i;
    }
  }
  return minPos;
}

Upvotes: 0

sprinter
sprinter

Reputation: 27996

You could do this with streams:

IntStream(0, list.length).min((i1, i2) -> list[i1] - list[i2]).get();

Upvotes: 0

camrydash
camrydash

Reputation: 93

You can add this method and pass in an integer array. It will return the index position of the integer with the lowest value in the array.

public static int getMinIndex(int[] array) {
    int minIndex = -1;
    int currentMinValue = Integer.MAX_VALUE;
    for(int i =0; i< array.length; i++) {
        if(array[i] < currentMinValue) {
            minIndex = i;
            currentMinValue = array[i];
        }
    }
    return minIndex;
}

Upvotes: 2

Louis Wasserman
Louis Wasserman

Reputation: 198481

    if (currMin > list[index])
        currMin = list[index];
    if (currMin > list[index])
        currPos = index;

If the first if condition is true, then by the time the second condition is checked, curMin will be exactly equal to list[index], so it will never be greater than...

You might want

     if (currMin > list[index]) {
        currMin = list[index];
        currPos = index;
     }

Upvotes: 3

Related Questions