Click here to Skip to main content
15,884,648 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, how can I add to existing code count of lines duplicate?
Thank you.

What I have tried:

Private Sub remove_duplicite(sender As Object, e As EventArgs)
        Dim sFiles() As String
        sFiles = Directory.GetFiles(filesPath1, remove_dupl)

        Dim path As String = String.Join("", sFiles)
        'MessageBox.Show(path)
        <pre> Dim i As Integer = 0


        Using sr As StreamReader = New StreamReader(path)
            Do While sr.Peek() >= 0
                lines.Add(sr.ReadLine())
                Dim sLine = sr.ReadLine

                If lines.Contains(sLine) Then
                    i += 1
                    pocet_duplicit = i
                    'lines.Add(i & "," & sLine)
                Else
                    lines.Add(sLine)
                End If
            Loop
        End Using

        'Write to file
        Using sw As StreamWriter = New StreamWriter(path)
            For Each line As String In lines
                sw.WriteLine(line)
            Next
        End Using
        Close()
    End Sub
Posted
Updated 25-Sep-22 1:26am
v2

Sort them: then identical lines are next to each other and simple to find and count.
 
Share this answer
 
Comments
Member 15251555 21-Sep-22 8:57am    
thi is a file with 140000 rows, I dont known how fast compare lines
OriginalGriff 21-Sep-22 9:59am    
"Sort them: then identical lines are next to each other and simple to find and count."
Member 15251555 22-Sep-22 2:59am    
Grif, do you have some examples ? I dont known how start.
OriginalGriff 22-Sep-22 3:17am    
Oh, come on!

You know how to read a file into an array of lines, yes? It's one line of code!
Member 15251555 22-Sep-22 5:10am    
Griff, I edited it what do you think, is it correct?
I'd use Linq[^]. See:

VB.NET
Dim lines As String() = New String(){"APPLE", "APPLE", "MANGO", "BANANA", "BANANA", "APPLE", "MANGO"}

Dim dups = lines. _
	Select(Function(l, i) New With{i, l}). _
	GroupBy(Function(x) x.l). _
	Select(Function(grp)  New With _
	{ _
		.Value=grp.Key,  _
		.Lines = String.Join(";", grp.Select(Function(x) x.i)) _
	}). _
	ToList()

C#
string[] lines = {"APPLE", "APPLE", "MANGO", "BANANA", "BANANA", "APPLE", "MANGO"};

var dups = lines
	.Select((l, i) => new {i, l})
	.GroupBy(x => x.l)
	.Select(grp => new
	{
		Value=grp.Key, 
		Lines = string.Join(";", grp.Select(x=> x.i))
	})
	.ToList();



dups variable holds:
Value Lines
APPLE 0;1;5
MANGO 2;6
BANANA 3;4
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900