LINQ Q. - find duplicate characters

A

Arthur Dent

Hello all,

I am trying to figure out how to do something in LINQ, IF I can even do it
in LINQ.
Online samples and such for LINQ is still a bit on the sketchy side.

Here is what I want to do...

I have some string, whatever, that doesn't matter. I want to find all the
characters which occur in the string more than once.
So say my string is "My string is cool.", I want run some query that will
return the letters [s, i, o] because they all occur multiple times.

Is this possible? What would be the right syntax for this?
Thanks for the help; this sure is a lot to learn --- but it is cool stuff!
:)
 
T

Teemu

Arthur Dent said:
Hello all,

I am trying to figure out how to do something in LINQ, IF I can even do it
in LINQ.
Online samples and such for LINQ is still a bit on the sketchy side.

Here is what I want to do...

I have some string, whatever, that doesn't matter. I want to find all the
characters which occur in the string more than once.
So say my string is "My string is cool.", I want run some query that will
return the letters [s, i, o] because they all occur multiple times.

Is this possible? What would be the right syntax for this?
Thanks for the help; this sure is a lot to learn --- but it is cool stuff!
:)

LINQ is nice, must it be done with it?

Dim s As String = "This contains duplicate characters"

Dim DuplicateChars As New List(Of String)
For i As Integer = 0 To s.Length - 1
If Not s.LastIndexOf(s.Substring(i, 1)) = i Then
If Not DuplicateChars.Contains(s.Substring(i, 1)) Then
DuplicateChars.Add(s.Substring(i, 1))
End If
End If
Next


There might be a nice way to do it with LINQ but it's not always necessary.

-Teemu
 
K

Kelly Ethridge

Hello,

I had to take this opportunity to learn some LINQ, so here is my first
attempt:

Sub Main()
Dim s = "My string is cool"
Dim r = From c In s Group By c Into Count() Where c <> " "
AndAlso Count > 1

For Each item In r
Debug.WriteLine(item)
Next
End Sub

Kelly
 
K

Kelly Ethridge

Oh, and if I has added a 'Select c' at the end then the result would
only include the character. Not the character and the count.


Kelly said:
Hello,

I had to take this opportunity to learn some LINQ, so here is my first
attempt:

Sub Main()
Dim s = "My string is cool"
Dim r = From c In s Group By c Into Count() Where c <> " "
AndAlso Count > 1

For Each item In r
Debug.WriteLine(item)
Next
End Sub

Kelly


Arthur said:
Hello all,

I am trying to figure out how to do something in LINQ, IF I can even
do it in LINQ.
Online samples and such for LINQ is still a bit on the sketchy side.

Here is what I want to do...

I have some string, whatever, that doesn't matter. I want to find all
the characters which occur in the string more than once.
So say my string is "My string is cool.", I want run some query that
will return the letters [s, i, o] because they all occur multiple times.

Is this possible? What would be the right syntax for this?
Thanks for the help; this sure is a lot to learn --- but it is cool
stuff! :)
 
A

Arthur Dent

I know not everything has to be done with the "latest/greatest", but I am
trying to learn LINQ,
and you know what they say about taking small bites, small steps;
I was just trying to take something small-scale and simple and use it as a
learning tool.

I do like your method though of stepping through and checking the first
index against the last index.
When I'd done this before, I'd stepped through, added each to a collection,
and checked each one to see if it already existed in the collection.
Yours is much more elegant than that.

Teemu said:
Arthur Dent said:
Hello all,

I am trying to figure out how to do something in LINQ, IF I can even do
it in LINQ.
Online samples and such for LINQ is still a bit on the sketchy side.

Here is what I want to do...

I have some string, whatever, that doesn't matter. I want to find all the
characters which occur in the string more than once.
So say my string is "My string is cool.", I want run some query that will
return the letters [s, i, o] because they all occur multiple times.

Is this possible? What would be the right syntax for this?
Thanks for the help; this sure is a lot to learn --- but it is cool
stuff! :)

LINQ is nice, must it be done with it?

Dim s As String = "This contains duplicate characters"

Dim DuplicateChars As New List(Of String)
For i As Integer = 0 To s.Length - 1
If Not s.LastIndexOf(s.Substring(i, 1)) = i Then
If Not DuplicateChars.Contains(s.Substring(i, 1)) Then
DuplicateChars.Add(s.Substring(i, 1))
End If
End If
Next


There might be a nice way to do it with LINQ but it's not always
necessary.

-Teemu
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top