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] )
minList.add(i);
i++;
}
logger.debug("Postition of min-item in arraylist {}",Arrays.toString(minList.toArray()));
return minList;
}