473,396 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

LINQ Q. - find duplicate characters

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!
:)

Dec 11 '07 #1
4 2628

"Arthur Dent" <hi*********************@yahoo.comkirjoitti viestissä
news:A7**********************************@microsof t.com...
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

Dec 11 '07 #2
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 Dent wrote:
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! :)
Dec 11 '07 #3
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 Ethridge wrote:
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 Dent wrote:
>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! :)
Dec 11 '07 #4
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" <ts*****@hotmail.comwrote in message
news:ST*******************@reader1.news.saunalahti .fi...
>
"Arthur Dent" <hi*********************@yahoo.comkirjoitti viestissä
news:A7**********************************@microsof t.com...
>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
Dec 11 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: Marc Gravell | last post by:
In Linq, you can apparently get a meaningful body from and expression's .ToString(); random question - does anybody know if linq also includes a parser? It just seemed it might be a handy way to...
1
by: David C | last post by:
I need to loop through an entire directory of folders and look for any that have the same 1st 4 characters. I know how to loop and read just the 1st 4 characters but I need some help determining...
1
by: news.microsoft.com | last post by:
Hello people: First of all forgiveness for my English. I am working with Linq to SQL, Visual Studio 2008 and SQL Server 2000. When you delete a record and then go back to Insert (ie, insert a...
2
by: news.microsoft.com | last post by:
Hello people: First of all forgiveness for my English. I am working with Linq to SQL, Visual Studio 2008 and SQL Server 2000. When you delete a record and then go back to Insert (ie, insert a...
9
by: =?Utf-8?B?RXZlcnQ=?= | last post by:
In my (Windows Forms) project I am using strongly typed datatables. Now I am trying to convert my 'rowfilter/group by' query logic to Linq. Most queries I can easily convert, but some I am having...
4
by: =?Utf-8?B?Tmljaw==?= | last post by:
Hello, I'm using LINQ to access a SQL Server database. The user needs to be able to duplicate a record. Is there an easy way to do this? I rather not have to set each property from one to the...
9
by: =?Utf-8?B?cmF1bGF2aQ==?= | last post by:
Hi all: after reading different places/sites about linq... I ran into these questions: 1. What framework do we need to run linq ? (does it depend on what version of visual studio we have?) how...
3
by: =?Utf-8?B?UGF1bCBQcmV3ZXR0?= | last post by:
I'm attempting to use LINQ to insert a record into a child table and I'm receiving a "Specified cast is not valid" error that has something to do w/ the keys involved. The stack trace is: ...
1
by: jbot | last post by:
I've got some pages that use linq to sql. When I look at the generated sql statements in sql server profiler, I see the same statements repeated. In the example below (from a linq data source...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.