Sep 25

One solution would be to loop through the array maintaining the smallest value found in a variable. Another solution would be to first sort the array, then the smallest value would be the first element.

int[] values = { 2, 67, 15, 3, 567 };

// first approach

int smallest = values[0];
for (int i=1; i<values.length; i++)
   smallest = Math.min(smallest, values[i]);

// second approach

smallest = values[0]; 

written by objects \\ tags: ,

2 Responses to “How do I find the smallest value in an array?”

  1. anak kankunk Says:

    singkat banget ya ..

    gmana lo kita di suruh ngoprasiin penjumlahan bilangan dengan type data masukan string.
    tapi operasinya pake int, lalu hasilnya disimpan dalam bentuk string lagi??

  2. gel Says:

    hi what will be the code if you are asking for 10 random numbers then you will get its average and will show 4 least differentt number that you have inputed

Leave a Reply

You must be logged in to post a comment.