-
Aug 16th, 2014, 04:51 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] [VB2010] Top string and its recourrences in a List by Linq
Using Linq, what is the fastest way to find in a List of String the string with most recourrences and the recourrences number?
i.e. ("a","b","e","a","f","h","e","a","m","b","a","v")
====> TopString = "a" -Recourrences = 4
Ty
Last edited by phil2000; Aug 16th, 2014 at 10:26 AM.
-
Aug 16th, 2014, 05:38 AM
#2
Hyperactive Member
Re: [VB2010] Top string and its recourrences in a List by Linq
Using arrays I made this.
Code:
Dim arr() As Char = {"a", "b", "e", "a", "f", "h", "e", "a", "m", "b", "a", "v"}
Dim i As Integer
Dim count_a As Integer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Me.i = 0 To 11
If arr(i) = "a" Then
count_a += 1
End If
Next
MsgBox("TopString = 'a' -Recourrences = " & count_a & "", vbInformation + vbOKOnly)
End Sub
-
Aug 16th, 2014, 05:55 AM
#3
Thread Starter
Hyperactive Member
Re: [VB2010] Top string and its recourrences in a List by Linq
Originally Posted by hamza.saleem
Using arrays I made this..........
Mate, but I DON'T KNOW what is the top letter... for this reason I would like to use Linq
-
Aug 16th, 2014, 06:05 AM
#4
Hyperactive Member
Re: [VB2010] Top string and its recourrences in a List by Linq
Originally Posted by phil2000
Mate, but I DON'T KNOW what is the top letter... for this reason I would like to use Linq
I am not familiar with LINQ. btw what is the top letter you don't know about maybe I can give some concept about that and for checking the occurrence loop is the fastest way.
-
Aug 16th, 2014, 06:18 AM
#5
Re: [VB2010] Top string and its recourrences in a List by Linq
How about this:
Code:
Dim l As New List(Of String) From {"b", "a", "e", "a", "f", "h", "e", "a", "m", "b", "a", "v"}
Dim ltrCts As IEnumerable = From s In l
Group s By Key = s Into Group
Select s = Key, lc = Group.Count() Order By lc Descending
Debug.WriteLine(ltrCts(0))
-
Aug 16th, 2014, 06:35 AM
#6
Thread Starter
Hyperactive Member
Re: [VB2010] Top string and its recourrences in a List by Linq
Originally Posted by dbasnett
How about this:
Code:
Dim l As New List(Of String) From {"b", "a", "e", "a", "f", "h", "e", "a", "m", "b", "a", "v"}
Dim ltrCts As IEnumerable = From s In l
Group s By Key = s Into Group
Select s = Key, lc = Group.Count() Order By lc Descending
Debug.WriteLine(ltrCts(0))
It works .......... and now in ltrCts(0) there are the right s=a letter and lc=4 number.
But (sorry I don't know Linq) HOW to store "s" into a string variable = "a" and "lc" into a integer variable = 4?
Last edited by phil2000; Aug 16th, 2014 at 06:47 AM.
-
Aug 16th, 2014, 07:13 AM
#7
Re: [VB2010] Top string and its recourrences in a List by Linq
add this class to get rid of the annonnymous type
Code:
Class someclass
Public theLtr As String
Public theCt As integer
End Class
and then change the other code to
Code:
Dim l As New List(Of String) From {"b", "a", "e", "a", "f", "h", "e", "a", "m", "b", "a", "v"}
Dim ltrCts As IEnumerable(Of someclass)
ltrCts = (From s In l Group s By Key = s Into Group
Select New someclass With {.theLtr = Key, .theCt = Group.Count()}).OrderByDescending(Function(ct) ct.theCt)
For Each foo As someclass In ltrCts
Debug.WriteLine("{0} - {1}", foo.theLtr, foo.theCt)
Next
-
Aug 16th, 2014, 07:45 AM
#8
Thread Starter
Hyperactive Member
Re: [VB2010] Top string and its recourrences in a List by Linq
Since letters are always 1 char long, I tried:
Code:
Dim Letter As String = Strings.Mid(ltrCts(0).ToString, 7, 1)
Dim Count As Integer = CInt(Strings.Mid(ltrCts(0).ToString, 15, 1))
...and it works fine.
-
Aug 16th, 2014, 10:20 AM
#9
Re: [VB2010] Top string and its recourrences in a List by Linq
Why must it be LINQ? It's not magical and very slow.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|