Suggestions for checking that all array values are unique
Posted
#1
(In Topic #1222)
Regular

Looking for suggestions. My Integer[] must only contain unique values. I just can't think of a really good way that does not involve a lot of unnecessary statistical manipulations.
tia
b
Posted
Guru

thatbruce said
Hi all.
Looking for suggestions. My Integer[] must only contain unique values. I just can't think of a really good way that does not involve a lot of unnecessary statistical manipulations.
tia
b
Off the top of my head I think just making a new list would be simplest….
Posted
Regular

I wasn't clear enough. I just need to check it not replace it. So have gone with
Anyone with other ideas? I feel that this is good enough as the length of the array is always < 25 items. But this will be called hundreds of times in a run, so I wonder if there is an efficiency weakness. I guess giving it a punt will give me some confidence.
b
Posted
Guru

thatbruce said
Ta.
I wasn't clear enough. I just need to check it not replace it. So have gone with
Anyone with other ideas? I feel that this is good enough as the length of the array is always < 25 items. But this will be called hundreds of times in a run, so I wonder if there is an efficiency weakness. I guess giving it a punt will give me some confidence.
b
aah i see well in that case probably be more efficient to finish checking and exit as soon as any duplicate is found rather than inspecting all the other items too.
Not sure how much more efficient it could be.
To ensure all are unique values you will have to inspect every element and make a note of every element inspected to match.
I tried another way, i thought to Sort the list first then any duplicates will be next to each other making searching simpler as can just check each item against the previous item and not build a new array..
with this code..
The profiler says the second method is faster.
Posted
Regular

In fact I can do this all inline in my project as I sort the array just beforehand so I can check that it is monotonic.
This is all to do with an old problem regarding keeping two arrays synchronized. I have run it through the profiler here and this is around 3 times faster than my old "manual" way of handling that.
A beer or two for you the next time I'm on an island of note.
b
Posted
Expert

It may shave off some more time in the process.
Cheers - Quin.
I code therefore I am
I code therefore I am
Posted
Guru

Quincunxian said
If you want the check to fail on the first detection of a duplicate, it may be better to do the check with a loop/repeat rather than a For/next so that it drops as soon as it fails rather than running through the entire array.
It may shave off some more time in the process.
It does exit on first detection.. <EMOJI seq="1f60e" tseq="1f60e">😎</EMOJI>
If aNewList[c - 1] = aNewList[c] Then Return
Posted
Expert

Cheers - Quin.
I code therefore I am
I code therefore I am
1 guest and 0 members have just viewed this.

