Max Capacity of IEnumerable(Of String)

kvin93 41 Reputation points
2021-07-12T20:24:32.463+00:00

Hello,

I use the code below to do some comparison among 2 strings (t1 and t2) :

  Dim ar1 As String() = Split(t1, ",")

  Dim ar2 As String() = Split(t2, ",")

  Dim txtoutput As IEnumerable(Of String) = ar2.Except(ar1)

When I tested the app with t1(almost 1250000 characters) and t2(almost half of the t1 length) , txtoutput returned 0 (but there were actually around 7000 items in common with the input strings).

What's the maximum safe length of input to this method ? What's the maximum count of IEnumerable(Of String) ?

Thanks

P.S project link :

1z3tADujRhUeZ4VXWFNqnNALj5sfGQSL4

My form :

Imports System.ComponentModel
Imports System.Linq
Imports System.Text

Public Class Form1
    Dim t1 As String
    Dim t2 As String
    Dim txtOutput As IEnumerable(Of String)
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim ofd As New OpenFileDialog
        ofd.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.Desktop
        ofd.Filter = "Text|*.txt"

        If ofd.ShowDialog = Windows.Forms.DialogResult.Cancel Then Exit Sub

        Try


            t1 = My.Computer.FileSystem.ReadAllText(ofd.FileName)

            oldLink.Text = Split(t1, ",").Length

            TextBox1.Text = ofd.FileName


        Catch ex As Exception

        End Try
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim ofd As New OpenFileDialog
        ofd.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.Desktop
        ofd.Filter = "Text|*.txt"

        If ofd.ShowDialog = Windows.Forms.DialogResult.Cancel Then Exit Sub

        Try


            t2 = My.Computer.FileSystem.ReadAllText(ofd.FileName)
            newLink.Text = Split(t2, ",").Length
            TextBox2.Text = ofd.FileName





        Catch ex As Exception

        End Try
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        If TextBox1.Text <> "" And TextBox2.Text <> "" And TextBox3.Text <> "" Then
            BackgroundWorker1.RunWorkerAsync()
        End If

    End Sub
    Sub getOutputCount()
        If InvokeRequired Then

            Me.Invoke(New MethodInvoker(AddressOf getOutputCount))
        Else
            finalLink.Text = txtOutput.Count
            Application.DoEvents()
        End If
    End Sub
    Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork

        Dim splt() As Char = {","c}
        Dim ar1 As List(Of String) = t1.Split(splt, StringSplitOptions.RemoveEmptyEntries).ToList
        Dim ar2 As List(Of String) = t2.Split(splt, StringSplitOptions.RemoveEmptyEntries).ToList
        ar1.Sort()
        ar2.Sort()
        txtOutput = ar2.Except(ar1)

        getOutputCount()


        Dim fTx2 As String = String.Join(",", txtOutput)




        My.Computer.FileSystem.WriteAllText(TextBox3.Text, fTx2, True)


    End Sub

    Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
        ProgressBar1.Value = e.ProgressPercentage

    End Sub

    Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
        MsgBox("Done!")
        Button3.Enabled = False
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        Dim ofd As New SaveFileDialog
        ofd.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.Desktop
        ofd.Filter = "Text|*.txt"

        If ofd.ShowDialog = Windows.Forms.DialogResult.Cancel Then Exit Sub

        Try


            TextBox3.Text = ofd.FileName





        Catch ex As Exception

        End Try
    End Sub

End Class
Developer technologies | VB
{count} votes

Accepted answer
  1. Viorel 122.6K Reputation points
    2021-07-13T15:39:00.987+00:00

    Using the published files, the next sample program:

    Dim t1 = File.ReadAllText("T1.txt")
    Dim t2 = File.ReadAllText("T2.txt")
    Dim ar1 = t1.Split(","c)
    Dim ar2 = t2.Split(","c)
    Dim result1 = ar2.Except(ar1)
    Dim result2 = ar1.Except(ar2)
    Dim result3 = ar1.Intersect(ar2)
    
    Console.WriteLine(ar1.Length)
    Console.WriteLine(ar1.Distinct().Count())
    Console.WriteLine(ar2.Length)
    Console.WriteLine(ar2.Distinct().Count())
    Console.WriteLine(result1.Count())
    Console.WriteLine(result2.Count())
    Console.WriteLine(result3.Count())
    

    gives these results:

    • 1332 items in ar1, no duplicates
    • 7066 items in ar2, 4548 unique items after removing duplicates
    • 3216 items in ar2 excepting items from ar1
    • 0 items in ar1 excepting items from ar2
    • 1332 common items in ar1 and ar2

    Maybe the confusion is caused by duplicates.


1 additional answer

Sort by: Most helpful
  1. Dewayne Basnett 1,381 Reputation points
    2021-07-12T20:51:34.677+00:00

    Try it like this

            Dim splt() As Char = {","c}
            Dim ar1 As List(Of String) = t1.Split(splt, StringSplitOptions.RemoveEmptyEntries).ToList
            Dim ar2 As List(Of String) = t2.Split(splt, StringSplitOptions.RemoveEmptyEntries).ToList
            ar1.Sort()
            ar2.Sort()
            Dim txtoutput As IEnumerable(Of String) = ar2.Except(ar1)
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.