Ed Kaim [MSFT] <(E-Mail Removed)> wrote:
> That algorithm wouldn't work if valid data isn't guaranteed. For example, if
> the user enters the array "0, 0, 0, 0, 15", it would return a false
> positive.
I think it depends on whether the numbers all have to be in the range
[1...max], in which case just checking that for each number and summing
is the efficient way of doing it.
> Although it's probably less efficient for smaller sets, this algorithm only
> requires a single pass:
>
> public static bool IsDataValid(int[] input)
>
> {
>
> Hashtable values = new Hashtable();
>
> for (int lcv = 0; lcv < input.Length; lcv++)
>
> {
>
> int val = input[lcv];
>
> if (values.ContainsKey(val) || (val < 1) || (val > input.Length))
I don't think that *really* counts as a single pass, as during each
"outer" pass you effectively need to go through all the numbers you've
already added. It's basically O(n log(n)) instead of O(n), assuming
hashtable lookup is O(log(n)).
--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too