Page 1 of 1

Suggestions for checking that all array values are unique

Posted: Tuesday 9th April 2024 9:03am
by thatbruce
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

Re: Suggestions for checking that all array values are unique

Posted: Tuesday 9th April 2024 5:59pm
by BruceSteers
thatbruce wrote: Tuesday 9th April 2024 9:03am 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....

Public sub Uniquify(aList as Integer[]) As Integer[]

  Dim aNewList As New Integer[]

  For c As Integer = 0 To aList.Max
    If Not aNewList.Exist(aList[c]) Then aNewList.Add(aList[c])
  Next

  Return aNewList

End

Re: Suggestions for checking that all array values are unique

Posted: Wednesday 10th April 2024 2:17am
by thatbruce
Ta.

I wasn't clear enough. I just need to check it not replace it. So have gone with

Public Function CheckArrayUniqueness(aList as Integer[]) As Boolean

  Dim aNewList As New Integer[]

  For c As Integer = 0 To aList.Max
    If Not aNewList.Exist(aList[c]) Then aNewList.Add(aList[c])
  Next

  Return aNewList.Count = aList.Count

End


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

Re: Suggestions for checking that all array values are unique

Posted: Wednesday 10th April 2024 8:41am
by BruceSteers
thatbruce wrote: Wednesday 10th April 2024 2:17am Ta.

I wasn't clear enough. I just need to check it not replace it. So have gone with

Public Function CheckArrayUniqueness(aList as Integer[]) As Boolean

  Dim aNewList As New Integer[]

  For c As Integer = 0 To aList.Max
    If Not aNewList.Exist(aList[c]) Then aNewList.Add(aList[c])
  Next

  Return aNewList.Count = aList.Count

End


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.

Public Function CheckArrayUniqueness(aList as Integer[]) As Boolean

  Dim aNewList As New Integer[]

  For c As Integer = 0 To aList.Max
    If aNewList.Exist(aList[c]) Then Return
    aNewList.Add(aList[c])
  Next

Return True

End


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

Public Function CheckArrayUniqueness2(aList As Integer[]) As Boolean

  Dim aNewList As Integer[] = aList.Copy().Sort()

  For c As Integer = 1 To aList.Max
    If aNewList[c - 1] = aNewList[c] Then Return 
  Next

Return True

End


with this code..

  Dim ai As New Integer[] 
  For c As Integer = 0 To 100
   ai.Add(Rand(0, 100))
  Next

Print CheckArrayUniqueness(ai)
Print CheckArrayUniqueness2(ai)



The profiler says the second method is faster.

Re: Suggestions for checking that all array values are unique

Posted: Wednesday 10th April 2024 10:02am
by thatbruce
Brilliant! (I think you might be getting a bit good with these prawns ;) )

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

Re: Suggestions for checking that all array values are unique

Posted: Wednesday 10th April 2024 10:57pm
by Quincunxian
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.

Re: Suggestions for checking that all array values are unique

Posted: Thursday 11th April 2024 8:32am
by BruceSteers
Quincunxian wrote: Wednesday 10th April 2024 10:57pm 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.. 😎

If aNewList[c - 1] = aNewList[c] Then Return

Re: Suggestions for checking that all array values are unique

Posted: Saturday 13th April 2024 10:19pm
by Quincunxian
Replied to an earlier version of the code Doh.