How to find the minimum value in an ArrayList, along with the index number? (Java)

JavaArraylist

Java Problem Overview


I need to get the index value of the minimum value in my arraylist in Java. MY arraylist holds several floats, and I'm trying to think of a way I can get the index number of the smallest float so I can use that index number elsewhere in my code. I'm a beginner, so please don't hate me. Thanks!

Java Solutions


Solution 1 - Java

You can use Collections.min and List.indexOf:

int minIndex = list.indexOf(Collections.min(list));

If you want to traverse the list only once (the above may traverse it twice):

public static <T extends Comparable<T>> int findMinIndex(final List<T> xs) {
	int minIndex;
	if (xs.isEmpty()) {
		minIndex = -1;
	} else {
		final ListIterator<T> itr = xs.listIterator();
		T min = itr.next(); // first element as the current minimum
		minIndex = itr.previousIndex();
		while (itr.hasNext()) {
			final T curr = itr.next();
			if (curr.compareTo(min) < 0) {
				min = curr;
				minIndex = itr.previousIndex();
			}
		}
	}
	return minIndex;
}

Solution 2 - Java

This should do it using built in functions.

public static int minIndex (ArrayList<Float> list) {
  return list.indexOf (Collections.min(list)); }

Solution 3 - Java

try this:

public int getIndexOfMin(List<Float> data) {
	float min = Float.MAX_VALUE;
	int index = -1;
	for (int i = 0; i < data.size(); i++) {
		Float f = data.get(i);
		if (Float.compare(f.floatValue(), min) < 0) {
			min = f.floatValue();
			index = i;
		}
	}
	return index;
}

Solution 4 - Java

There is an easier way to find a min integer in array list:

int min = array.get(0);
        for (int i : array){
            min = min < i ? min : i;
        }

Solution 5 - Java

public static int minIndex (ArrayList<Float> list) {
  return list.indexOf (Collections.min(list));
 }
System.out.println("Min = " + list.get(minIndex(list));

Solution 6 - Java

  1. Declare a arraylist with Floats.

  2. Collection.min() - finding the minimum element in the list.

  3. List.indexOf() - finding the index of the minimum element.

public class Test {

	public static void main(String[] args) {

		ArrayList<Float> ary = new ArrayList<Float>();
		ary.add((float) 3.0);
		ary.add((float) 6);
		ary.add((float) 2);
		ary.add((float) 1.3);
		ary.add((float) 4.2);
		int indx = minIndex(a);
		System.out.println(indx);
	}

	public static int minIndex(ArrayList<Float> list) {
		return list.indexOf(Collections.min(list));
	}

}

Solution 7 - Java

You have to traverse the whole array and keep two auxiliary values:

  • The minimum value you find (on your way towards the end)
  • The index of the place where you found the min value

Suppose your array is called myArray. At the end of this code minIndex has the index of the smallest value.

var min = Number.MAX_VALUE; //the largest number possible in JavaScript
var minIndex = -1;

for (int i=0; i<myArray.length; i++){
   if (myArray[i] < min){
      min = myArray[i];
      minIndex = i;
   }
}

This is assuming the worst case scenario: a totally random array. It is an O(n) algorithm or order n algorithm, meaning that if you have n elements in your array, then you have to look at all of them before knowing your answer. O(n) algorithms are the worst ones because they take a lot of time to solve the problem.

If your array is sorted or has any other specific structure, then the algorithm can be optimized to be faster.

Having said that, though, unless you have a huge array of thousands of values then don't worry about optimization since the difference between an O(n) algorithm and a faster one would not be noticeable.

Solution 8 - Java

Here's what I do. I find the minimum first then after the minimum is found, it is removed from ArrayList.

ArrayList<Integer> a = new ArrayList<>();
a.add(3);
a.add(6);
a.add(2);
a.add(5);

while (a.size() > 0) {
    int min = 1000;
    for (int b:a) {
        if (b < min)
            min = b;
    }
    System.out.println("minimum: " + min);
    System.out.println("index of min: " + a.indexOf((Integer) min));
    a.remove((Integer) min);
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2130496View Question on Stackoverflow
Solution 1 - JavaMarimuthu MadasamyView Answer on Stackoverflow
Solution 2 - JavaGoZonerView Answer on Stackoverflow
Solution 3 - JavaBlackJokerView Answer on Stackoverflow
Solution 4 - JavaBaurzhan KozhaevView Answer on Stackoverflow
Solution 5 - JavaM E S A B OView Answer on Stackoverflow
Solution 6 - JavaIndhujaView Answer on Stackoverflow
Solution 7 - JavacockypupView Answer on Stackoverflow
Solution 8 - JavaArwan KhoiruddinView Answer on Stackoverflow