Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27351

Finding and COUNTING duplicate items in a List WITHOUT grouping them together.

$
0
0
Hi there, I've come to yet another hiccup with my new project, and that is, I want to count the number of times a specific string (In this case a SHA1 Hash) occurs in a List.

BUT, I don't want to group my list as this will adversely affect the validity of the data when I save each Hash to file.
So for example lets say my list looks similar to this:
  • A
  • B
  • C
  • D
  • A
  • A
  • E


I have tried to use
Code:

        Dim groups = FileHashes.GroupBy(Function(value) value)
        For Each grp In groups
            HashDuplicates.Add(grp.Count)
            Debug.Print(grp(0) & " - " & grp.Count)
        Next

But his results in:
  • A - 3
  • B - 1
  • C - 1
  • D - 1
  • E - 1


The output I'm looking for would be:
  • A - 3
  • B - 1
  • C - 1
  • D - 1
  • A - 3
  • A - 3
  • E - 1


My feeble attempt: (Please don't laugh!)
Code:

        For Each item In FileHashes
            Dim count As Integer = 0
            Dim i As Integer = 0
            Do Until i = FileHashes.Count - 1
                If item = FileHashes(i) Then
                    count += 1
                    HashDuplicates.Add(count)
                Else
                    HashDuplicates.Add(1)
                End If
            Loop
            Debug.Print(item + " checked")
        Next

Any ideas?

Viewing all articles
Browse latest Browse all 27351

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>