Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lowest values of an array if more than one result

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
like image 519
tattykeeran Avatar asked Dec 02 '25 03:12

tattykeeran


2 Answers

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

 }
}
like image 50
Scription Avatar answered Dec 04 '25 17:12

Scription


One thing can be "Sort" your array in ascending order & then display values from the start till they are equal

like image 38
Amit B Avatar answered Dec 04 '25 15:12

Amit B