Suggestions for checking that all array values are unique

Post your Gambas programming questions here.
Post Reply
User avatar
thatbruce
Posts: 168
Joined: Saturday 4th September 2021 11:29pm

Suggestions for checking that all array values are unique

Post 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
Have you ever noticed that software is never advertised using the adjective "spreadable".
User avatar
BruceSteers
Posts: 1585
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Suggestions for checking that all array values are unique

Post 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
If at first you don't succeed , try doing something differently.
BruceS
User avatar
thatbruce
Posts: 168
Joined: Saturday 4th September 2021 11:29pm

Re: Suggestions for checking that all array values are unique

Post 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
Have you ever noticed that software is never advertised using the adjective "spreadable".
User avatar
BruceSteers
Posts: 1585
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Suggestions for checking that all array values are unique

Post 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.
If at first you don't succeed , try doing something differently.
BruceS
User avatar
thatbruce
Posts: 168
Joined: Saturday 4th September 2021 11:29pm

Re: Suggestions for checking that all array values are unique

Post 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
Have you ever noticed that software is never advertised using the adjective "spreadable".
User avatar
Quincunxian
Posts: 173
Joined: Sunday 25th June 2017 12:14am
Location: Western Australia

Re: Suggestions for checking that all array values are unique

Post 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.
Cheers - Quin.
I code therefore I am
User avatar
BruceSteers
Posts: 1585
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Suggestions for checking that all array values are unique

Post 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
If at first you don't succeed , try doing something differently.
BruceS
User avatar
Quincunxian
Posts: 173
Joined: Sunday 25th June 2017 12:14am
Location: Western Australia

Re: Suggestions for checking that all array values are unique

Post by Quincunxian »

Replied to an earlier version of the code Doh.
Cheers - Quin.
I code therefore I am
Post Reply