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&lt;Integer&gt; getMinIndex(double... arrayList){
    int minIndex = 0;
    int i = 0; 
    int len = arrayList.length;
    while(i &lt; len){
        if(arrayList[i] &lt; arrayList[minIndex]  )
            minIndex = i;
        i++;
    }

    //Below is specifically for case where are multiple minimums and we want all of them
    i=0;
    List&lt;Integer&gt; minList = new ArrayList&lt;Integer&gt;();
    while(i &lt; len){
        if(arrayList[i] == arrayList[minIndex]  )
            minList.add(i);
        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&lt;Integer&gt; list = new ArrayList&lt;&gt;();

for (int i = 0; i &lt; array.length ; i++) {
    if (array[i] == array[minIndex]) {
        list.add(i);
    } else if (array[minIndex] &gt; array[i]) {
        list.clear();
        list.add(i);

        minIndex = i;
    }
}

return list;


Changes and recommendations:


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
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
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

As additional recommendations I would add:


I think that 'find' could be used as prefix instead of 'get', since you are actually searching for those indexes, you don't getting them from some POJO
I also think that it is better if you are using curly brackets even in a single line block statements
Also there is no need of transforming the list into array and then into string in the debug log. Lists has nice implementation of toString function that would print the elements the same way as Arrays.toString