By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,306 Members | 1,621 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,306 IT Pros & Developers. It's quick & easy.

Duplicate value in an array

P: n/a
spl
What is the fastest way to find a duplicate value in an array of 10
elements. I have to find just any one first occurrence of the
duplication.
Lets say I have ary[10]={10, 20, 40, 90, 30, 60, 35, 40, 85, 90}. Here
if I have to 40 is duplicated, that's fine, I don't required to find
90 is also repeating. I don't want to use STLs.

Can suggest any fast algorithm for this?
Feb 6 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On 2008-02-06 11:06:37 -0500, spl <sp**********@gmail.comsaid:
What is the fastest way to find a duplicate value in an array of 10
elements. I have to find just any one first occurrence of the
duplication.
Lets say I have ary[10]={10, 20, 40, 90, 30, 60, 35, 40, 85, 90}. Here
if I have to 40 is duplicated, that's fine, I don't required to find
90 is also repeating. I don't want to use STLs.

Can suggest any fast algorithm for this?
For 10 elements, just about any algorithm will be fast. Brute force is
the simplest. Start with the first element and scan the remaining
elements for a duplicate. If you don't find one, move on to the next
element. Repeat until done.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Feb 6 '08 #2

P: n/a

"spl" <sp**********@gmail.comwrote:
What is the fastest way to find a duplicate value in an array of 10
elements. I have to find just any one first occurrence of the
duplication.
Lets say I have ary[10]={10, 20, 40, 90, 30, 60, 35, 40, 85, 90}. Here
if I have to 40 is duplicated, that's fine, I don't required to find
90 is also repeating. I don't want to use STLs.

Can suggest any fast algorithm for this?
Yes. Use a pair of nested "for" loops to compaire each element to
all of the elements to its right. Set a flag and break if a
match is found. Return the flag.

--
Cheers,
Robbie Hatley
lonewolf aatt well dott com
www dott well dott com slant user slant lonewolf slant
Feb 7 '08 #3

P: n/a
spl
Thanks for the suggestions. Its done.
Feb 7 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.