blah blah blah is here! blah blah » Close

up0down
link

Does anyone know a way to check a string array for unique values without resorting to the bubblesort-like method of checking each element against the rest? my current pseudocode reads:
loop x
subloop y
check value x against value y, if match then fail
end subloop y
end loop x
but it seems rather inefficient for my purpose.

last answered 2 years ago

1 answers

up0down
link

Method A
1. sort the array
2. run over the sorted array and validate each value is different then the next one
Method B
Add all the items to a HahTable - validating foreach value it was not already added
Cheers

up0down
link

If I understand you correctly, wich I doubt, you want to do something like this:
ar = array
// Loop through the array
for (int i = 1; i < ar.Count; i++)
{
int p = i + 1;
bool excists = false;

// Make another loop that runs up to the loop nr your at now
for (int x = 0; x < i; x++)
{
// Check if any number up to the current number has the same value
if (ar[i] == ar[x])
{
excists = true;
}
}
if (excists == false)
{
// it does not excist backward so do what you want
}
else
{
// it excists backward, so do what you want
}
}
I have done this on about 100 posts without any notacble performance sink, but I bet it can be kinda horrible on 500+

This post was imported from csharpfriends, if you have a similiar question please ask it again.

All previous members have been migrated, hope you enjoy the new platform!

Feedback