Create a text file called TOONS.TXT with the following data:
Daffy Duck
Daffy Duck
Daisy Duck
Fred Flintstone
Mighty Mouse
Minnie Mouse
Porky Pig
Yosemite Sam

Write a program to find and display those names in the TOONS.TXT file that are repeated. Assume that the file has already been sorted into alphabetical order. When a name is found to be repeated, display it only once.

Having trouble writing this program, can anyone help?

Dani AI

Generated

A simple, robust approach is to walk the sorted file once and compare each line to the previous line. When the current line equals the previous line (use a case-insensitive comparison and Trim to remove stray spaces) add it to a small set of duplicates only if it was not already added. This handles names repeated any number of times while ensuring each repeated name is shown only once.

The existing posts show the common pitfalls: ’s StreamReader loop looks for a single name rather than detecting adjacent duplicates, and ’s ReadAllLines + Contains logic builds a unique list of names instead of listing only those that are repeated. The following compact pattern implements the adjacent-compare approach and stores duplicates exactly once.

Public Sub ShowRepeatedNames(filePath As String, outputList As ListBox)
    If Not IO.File.Exists(filePath) Then Return
    Dim seenDuplicates As New HashSet(Of String)(StringComparer.OrdinalIgnoreCase)

    Using sr As New IO.StreamReader(filePath)
        Dim prev As String = Nothing
        While Not sr.EndOfStream
            Dim cur As String = sr.ReadLine().Trim()
            If cur = "" Then Continue While
            If prev IsNot Nothing AndAlso String.Equals(cur, prev, StringComparison.OrdinalIgnoreCase) Then
                If seenDuplicates.Add(cur) Then outputList.Items.Add(cur)
            End If
            prev = cur
        End While
    End Using
End Sub

Notes and troubleshooting: ensure the file really is sorted (otherwise use a Dictionary(Of String,Integer) or LINQ GroupBy to count occurrences); trim lines and skip empty lines; prefer the Using pattern so the file is closed automatically; and choose case-insensitive comparison if “Daffy Duck” vs “daffy duck” should be treated the same. For very large unsorted files, an external sort or a disk-backed counting solution is necessary to avoid excessive memory use.

Recommended Answers

All 6 Replies

text file is created just need assistance with the program.

@Stats, you will definitely have to show some more effort. What code do you have so far? The question looks like it has been copied directly from a homework task. We WILL help, if you show some effort.

Sorry, new to the fourm
This is what I have:

Dim name As String = " "
    Dim sr As IO.StreamReader = IO.File.OpenText("toons.txt")
    Do While (name <> txtName.Text) And (sr.Peek <> -1)
      name = sr.ReadLine
    Loop
    If (name = txtName.Text) Then
      txtNotify.Text = "Name located: " & " " & name
    Else
      txtNotify.Text = "Sorry - the name not located."
    End If
    sr.Close()
  End Sub

stats79,, are u using vb.net or vb6?
streamreader is a function in vb.net not in vb6..

VB Express

See if this helps.
2 TextBoxes(MultiLine = True)

Public Class Form1
    Private myCoolToonsFile As String = "C:\toons.txt" '// your File.

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If IO.File.Exists(myCoolToonsFile) Then '// check if file exists.
            Dim arTemp() As String = IO.File.ReadAllLines(myCoolToonsFile) '// Load File in a String Array, which loads each line as a individual array.
            For Each fileLine As String In arTemp '// loop thru all arrays.
                With TextBox1 '// loads all file lines.
                    If Not .Text = "" Then .Text &= vbNewLine & fileLine Else .Text = fileLine '// add line.
                End With
                With TextBox2 '// loads only file lines that do not repeat.
                    If Not .Text.Contains(fileLine) Then '// check if TextBox does Not Contain the file line.
                        If Not .Text = "" Then .Text &= vbNewLine & fileLine Else .Text = fileLine '// add line.
                    End If
                End With
            Next
        End If
    End Sub
End Class
commented: Nicely executed. +6
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.