# Finding lowest non-zero number in an array

Page 1 of 1

## 3 Replies - 7850 Views - Last Post: 07 January 2010 - 12:08 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=148652&amp;s=ed222e0b53c1ee70e4154d7e3e52d33b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 irv_the_swerve

Reputation: 0
• Posts: 12
• Joined: 03-November 09

# Finding lowest non-zero number in an array

Posted 07 January 2010 - 08:53 AM

Hey guys, hope you all had a good Christmas and new year

I have a quick (probably easy) question about reading values in an array...I've been working on a program for 6 hours today and my brain has suddenly deserted me

I want to basically print/select the lowest number in an array, which is non-zero.

For example if the array was:

X[5]

and the elements were:

[0 0 3 4 5]

I would like it to select the minimum (3 in this case) and enter it into (for example) minX.

Thanks

This post has been edited by irv_the_swerve: 07 January 2010 - 08:57 AM

Is This A Good Question/Topic? 0

## Replies To: Finding lowest non-zero number in an array

### #2 g00se

• D.I.C Lover

Reputation: 3550
• Posts: 16,190
• Joined: 20-September 08

## Re: Finding lowest non-zero number in an array

Posted 07 January 2010 - 09:07 AM

Sort the array, then scan it for first element > 0

### #3 DillonSalsman

Reputation: 13
• Posts: 144
• Joined: 30-October 07

## Re: Finding lowest non-zero number in an array

Posted 07 January 2010 - 11:56 AM

Well.. damn that was easy
My brain having not done anything productive this morning started to come up with a horrible solution.

### #4 Locke

• Sarcasm Extraordinaire!

Reputation: 550
• Posts: 5,624
• Joined: 20-March 08

## Re: Finding lowest non-zero number in an array

Posted 07 January 2010 - 12:08 PM

You don't even have to sort it...

```int minX = Integer.MAX_VALUE;

for (int i = 0; i < X.length; i++)
{
if (X[i] != 0 && X[i] < minX)
minX = X[i];
}
```

This post has been edited by Locke: 07 January 2010 - 12:15 PM