I need to find the lowest value in an array, but I want to know how to handle multiple results. Say my array contains [1,4,7,5,3,1] - my result should be 1,1. Any ideas?
double minimum = array1[0]; //sets the first to be the smallest
for (int i = 0; i < array1.length; i++) //goes through your array
{
if (array1[i] < array1[0]) //checks and replaces if necessary
{
minimum = array[i];
}
}
System.out.println( minimum ); //returns the value of the smallest
you have a small mistake in your code, you should have compare the current value to the minimum and not to the first value
double minimum = array1[0]; //sets the first to be the smallest
var minValueCounter = 0;
for (int i = 0; i < array1.length; i++) //goes through your array
{
if (array1[i] < minimum) //checks and replaces if necessary
{
minimum = array[i];
minValueCounter = 1;
}
else if (array1[i] == minimum) //checks and replaces if necessary
{
minValueCounter++;
}
}
One thing can be "Sort" your array in ascending order & then display values from the start till they are equal
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With