Duplicate value in an array

S

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?
 
P

Pete Becker

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.
 
R

Robbie Hatley

spl said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top