Best way to check if a string contains all of the integers from 1 to N with no duplicates?

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,532
Office Version
  1. 365
Platform
  1. Windows
I am working on a UDF to generate a random set of counting numbers on [1,N]. For example, "3 2 5 4 1".

Now I want to write another UDF to test it. What I have come up with is to split the result string into an array, create another array of the same length, loop through the result array setting a code in the second array. If I find a code already set, the code fails. Here's my code. Is there a better way?

VBA Code:
Function TestRandSelect(pM As Long, pN As Long) As Variant

Dim iTally As Long
Dim Result As Variant
Dim ResultArray As Variant
Dim TallyArray() As Variant
Dim NextRand As Long

ReDim TallyArray(1 To pN)

' To be done in a loop
  Result = RandSelect1(pM, pN)
  ResultArray = Split(Result)
  For iTally = 1 To pM
    NextRand = ResultArray(iTally)
    If TallyArray(NextRand) <> "" Then
      TestRandSelect = "Error"
      Exit Function
    End If
    TallyArray(NextRand) = "X"
  Next iTally
  
TestRandSelect = "OK"

End Function
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
use a dictionary, that's made for that.
But if it's good generated, you don't need the test
Random select N number between 1 and N (exact same, thus normally without duplicates).
If the sum of them is N * (N+1) / 2 then there are no duplicates.
 
Upvote 0
I don't have the RandSelect1 function so I have structured this a different way but you should be able to adapt to your structure.
This requires no looping & no dictionary.

VBA Code:
Function Test(Result) As String
  Dim a As Variant, b As Variant
  
  a = Split(Result)
  b = Application.Transpose(Evaluate("row(1:" & UBound(a) + 1 & ")"))
  Test = "Error"
  If Join(b) = Join(WorksheetFunction.Sort(Split(Result), , , True)) Then Test = "OK"
End Function

I tested it by feeding strings directly like this but you could use the strings from your RandSelect1 function/

VBA Code:
Sub Test_Function_Test()
  MsgBox Test("3 2 5 4 1")
End Sub
 
Upvote 0
WorksheetFunction.Sort is what ? Since excel-2019 or 2021 ?
 
Upvote 0
WorksheetFunction.Sort is what ? Since excel-2019 or 2021 ?
1648981998945.png
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,198
Members
449,072
Latest member
DW Draft

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top