954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

java code to find lowest and unique number from aray

hey can any one help me??

i have 2 develop a code to find the "lowest and unique" number from a array..i thot of sorting them and then comparing the lowest number with othe numbers but i m feelin it wont work...pls help me out...

dhr
Newbie Poster
5 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

Method 1:
-----------------
1. sort ur array
2. make a string as csv from the array elements
3. for each number in ur sorted array, check if the indexOf() and lastIndexOf() from the string return same values. Return at the first number that does so, that's ur answer.
Method 2:
--------------
In step 1 of method 1, don't store duplicates and the smallest number shall be answer after sorting itself.

Method 3:
--------------
1. Make a wrapper of all ur numbers and insert in a collection.
2. use the collection's sort method to sort them.
3. check for every number if it exists at some other index as well as the one being checked. Return the first number that dissatisfies the condition

hkansal
Newbie Poster
21 posts since Mar 2009
Reputation Points: 10
Solved Threads: 3
 

After sorting the array, you just have to compare each entry with the one before and the one after it. The first one that's different from the ones both sides is the smallest unique value.

JamesCherrill
Posting Genius
Moderator
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

for efficiency, apply method 2 above. The sorting operation does it all

hkansal
Newbie Poster
21 posts since Mar 2009
Reputation Points: 10
Solved Threads: 3
 

thanks a lot...i got the answer...thanx again..

dhr
Newbie Poster
5 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

You can use a while loop to implement the solution James suggested. Be careful not to try to indexes that are not actually in the array. And mark your threads as solved when you are satisfied with the help you've been given, or when you've found a solution, as you indicated above.

BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You