• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 234
  • Last Modified:

Determine min array element no?

I want to know is there any method provided in Java to help us find the array element no. which has the smallest value among all. Eg. A[0] = 1, A[1] = 3, A[2] = 0. Therefore, the smallest value is '0', and the element no is '2'. Thus, can I get the element no '2' by using any easy method.

1 Solution
 There is no pre-existing method that will accomplish what you want, but you could write one yourself.  Try using this code.

int findSmallestIndex( int arr[], int length )
  int nSmallVal = Integer.MAX_VALUE;
  int nSmallIndex = 0;

  for ( int n = 0; n < length; n++ )
    if ( arr[ n ] < nSmallVal )
       nSmallVal = arr[ n ];
       nSmallIndex = n;
  return nSmallIndex;

  When multiple entries have the same value, the first one encountered will always win.  This is a brute force approach to this problem, but will be acceptable as long as you don't need to store a very large number of elements, and will not be calling this function a large number of times.  In those cases, I would suggest presorting the array, and simply looking at the value of the first element (if this fits into your needs).
hi yuho,

you can dig around for books with the kind of sorting algorithms you need, even to sorting objects. Try this one:

Java Algorithms by Scott Robert Ladd,Mc Graw Hill 1997
yuhoAuthor Commented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now