Select multiple minimum items in an Array

I have an array and need to identify minimum value in the array - This is easy. However, this array can have duplicates and there exist multiple minimum values and we need to identify them.

The following code works and is done through 2 loops. First loop identifies the minimum value and the second loop all values which equate to the minimum value.

Please let me know your thoughts and if any other efficient way is possible.

public static List<Integer> getMinIndex(double... arrayList){
int minIndex = 0;
int i = 0;
int len = arrayList.length;
while(i < len){
if(arrayList[i] < arrayList[minIndex]  )
minIndex = i;
i++;
}

//Below is specifically for case where are multiple minimums and we want all of them
i=0;
List<Integer> minList = new ArrayList<Integer>();
while(i < len){
if(arrayList[i] == arrayList[minIndex]  )
i++;
}
logger.debug("Postition of min-item in arraylist {}",Arrays.toString(minList.toArray()));
return minList;
}


You can achieve what you want, just with one for loop:

int minIndex = 0;
List<Integer> list = new ArrayList<>();

for (int i = 0; i < array.length ; i++) {
if (array[i] == array[minIndex]) {
} else if (array[minIndex] > array[i]) {
list.clear();

minIndex = i;
}
}

return list;


Changes and recommendations:

1. I think that you must change the name of the method variable to 'array', since there is a Java collection class named ArrayList and it could confuse some readers of your code
2. You should also change the method name, since from its name it is not clear that it returns a list of min indexes. Maybe getMinIndexes could be used
3. Regarding the old code, I think that you should use less temporal variables, in your case len variable, doesn't give any more semantic to the code, so it could be removed. When you works with arrays consider the usage of for loops.

Update

• Good answer. I'd just get rid of minIndex/ array[minIndex] and track the minimal value directly instead starting with double minValue = Double.MAX_VALUE;. – RoToRa Aug 28 '17 at 12:17