C# find the count of duplicate characters in an array and list them
up vote
0
down vote
favorite
I am trying to find the number of duplicate characters in an array and list them. For example, the user enters "this is my house", the output should look like this:
Number of duplicate characters is: 3 and the duplicates are: h i s
I have to use ToCharArray()
I've been trying but I can't get it to work properly, can you please help?
Thanks
Here is my code:
using System;
class Program
static void Main()
Console.Write("type a sentence: ");
String str = Console.ReadLine();
char arr = str.ToCharArray();
for (int j = 0; j < arr.Length; j++)
for (int k = j + 1; k < arr.Length; k++)
if (arr[j] == arr[k] && j != k)
Console.WriteLine("number of duplicates: " + k + "n" + "duplicates are: " + arr[j]);
Console.ReadLine();
c# arrays duplicates
add a comment |
up vote
0
down vote
favorite
I am trying to find the number of duplicate characters in an array and list them. For example, the user enters "this is my house", the output should look like this:
Number of duplicate characters is: 3 and the duplicates are: h i s
I have to use ToCharArray()
I've been trying but I can't get it to work properly, can you please help?
Thanks
Here is my code:
using System;
class Program
static void Main()
Console.Write("type a sentence: ");
String str = Console.ReadLine();
char arr = str.ToCharArray();
for (int j = 0; j < arr.Length; j++)
for (int k = j + 1; k < arr.Length; k++)
if (arr[j] == arr[k] && j != k)
Console.WriteLine("number of duplicates: " + k + "n" + "duplicates are: " + arr[j]);
Console.ReadLine();
c# arrays duplicates
github.com/morelinq/MoreLINQ/issues/125 may be of interest.
– mjwills
Nov 11 at 6:30
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am trying to find the number of duplicate characters in an array and list them. For example, the user enters "this is my house", the output should look like this:
Number of duplicate characters is: 3 and the duplicates are: h i s
I have to use ToCharArray()
I've been trying but I can't get it to work properly, can you please help?
Thanks
Here is my code:
using System;
class Program
static void Main()
Console.Write("type a sentence: ");
String str = Console.ReadLine();
char arr = str.ToCharArray();
for (int j = 0; j < arr.Length; j++)
for (int k = j + 1; k < arr.Length; k++)
if (arr[j] == arr[k] && j != k)
Console.WriteLine("number of duplicates: " + k + "n" + "duplicates are: " + arr[j]);
Console.ReadLine();
c# arrays duplicates
I am trying to find the number of duplicate characters in an array and list them. For example, the user enters "this is my house", the output should look like this:
Number of duplicate characters is: 3 and the duplicates are: h i s
I have to use ToCharArray()
I've been trying but I can't get it to work properly, can you please help?
Thanks
Here is my code:
using System;
class Program
static void Main()
Console.Write("type a sentence: ");
String str = Console.ReadLine();
char arr = str.ToCharArray();
for (int j = 0; j < arr.Length; j++)
for (int k = j + 1; k < arr.Length; k++)
if (arr[j] == arr[k] && j != k)
Console.WriteLine("number of duplicates: " + k + "n" + "duplicates are: " + arr[j]);
Console.ReadLine();
c# arrays duplicates
c# arrays duplicates
asked Nov 11 at 4:53
Nas
72
72
github.com/morelinq/MoreLINQ/issues/125 may be of interest.
– mjwills
Nov 11 at 6:30
add a comment |
github.com/morelinq/MoreLINQ/issues/125 may be of interest.
– mjwills
Nov 11 at 6:30
github.com/morelinq/MoreLINQ/issues/125 may be of interest.
– mjwills
Nov 11 at 6:30
github.com/morelinq/MoreLINQ/issues/125 may be of interest.
– mjwills
Nov 11 at 6:30
add a comment |
3 Answers
3
active
oldest
votes
up vote
4
down vote
You can try to use linq instead of for loop.
GroupBy
make a group and use where
get count
greater than 1.
var r= str.Replace(" ", "").GroupBy(_ => _).Where(x => x.Count() > 1).Select(x => x.Key);
then use string.Join
method and linq count
instead of a loop to get your expect result.
Console.Write("type a sentence: ");
String str = Console.ReadLine();
var result = str.Replace(" ", "")
.GroupBy(_ => _)
.Where(x => x.Count() > 1)
.Select(x => x.Key);
Console.WriteLine("number of duplicates: " + result.Count() + "r" + "duplicates are: " + string.Join(" ",result));
c# online
Result
type a sentence: number of duplicates: 3 duplicates are: h i s
thanks but I can't use Linq, this is an assignment and we are limited, I have to use a simple code like the above, my issue is that I can't get it to count or list properly.
– Nas
Nov 11 at 5:02
@Nas Ok I edit my answer it can show like your expect result
– D-Shih
Nov 11 at 5:03
add a comment |
up vote
0
down vote
Here is another solution without linq:
static void Main(string args)
string longText = @"your sentence comes here";
foreach (var character in CharacterCount.Count(longText))
if(character.Value>1)
Console.WriteLine("0 - 1", character.Key, character.Value);
class CharacterCount
public static SortedDictionary<char, ulong> Count(string stringToCount)
SortedDictionary<char, ulong> characterCount = new SortedDictionary<char, ulong>();
foreach (var character in stringToCount)
if (!characterCount.ContainsKey(character))
characterCount.Add(character, 1);
else
characterCount[character]++;
return characterCount;
Consider usingTryGetValue
rather thanContainsKey
.
– mjwills
Nov 11 at 8:53
add a comment |
up vote
0
down vote
This may help.
Console.Write("type a sentence: ");
String str = Console.ReadLine();
char arr = str.ToCharArray();
int count = new int[10000];
for (int j = 0; j < arr.Length; j++)
int ans=0;
for (int j = 0; j < count.Length; j++)
if(count[j]>1)
ans++;
Console.Write("Number of duplicates: " + ans +" duplicates are: ");
for (int j = 0; j < count.Length; j++)
if(count[j]>1)
Console.Write((char)j +" " );
Console.WriteLine();
}
Thanks Chameera, it works, just couple of notes, it is taking the Space as a character (for example "this is my house" is showing as 4 duplicates instead of 3), how can we avoid that. and also is it possible to list the duplicates on the same line instead of each on a separate line?
– Nas
Nov 11 at 5:40
@Nas Hope this will solve your problem
– Chameera
Nov 16 at 11:51
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
You can try to use linq instead of for loop.
GroupBy
make a group and use where
get count
greater than 1.
var r= str.Replace(" ", "").GroupBy(_ => _).Where(x => x.Count() > 1).Select(x => x.Key);
then use string.Join
method and linq count
instead of a loop to get your expect result.
Console.Write("type a sentence: ");
String str = Console.ReadLine();
var result = str.Replace(" ", "")
.GroupBy(_ => _)
.Where(x => x.Count() > 1)
.Select(x => x.Key);
Console.WriteLine("number of duplicates: " + result.Count() + "r" + "duplicates are: " + string.Join(" ",result));
c# online
Result
type a sentence: number of duplicates: 3 duplicates are: h i s
thanks but I can't use Linq, this is an assignment and we are limited, I have to use a simple code like the above, my issue is that I can't get it to count or list properly.
– Nas
Nov 11 at 5:02
@Nas Ok I edit my answer it can show like your expect result
– D-Shih
Nov 11 at 5:03
add a comment |
up vote
4
down vote
You can try to use linq instead of for loop.
GroupBy
make a group and use where
get count
greater than 1.
var r= str.Replace(" ", "").GroupBy(_ => _).Where(x => x.Count() > 1).Select(x => x.Key);
then use string.Join
method and linq count
instead of a loop to get your expect result.
Console.Write("type a sentence: ");
String str = Console.ReadLine();
var result = str.Replace(" ", "")
.GroupBy(_ => _)
.Where(x => x.Count() > 1)
.Select(x => x.Key);
Console.WriteLine("number of duplicates: " + result.Count() + "r" + "duplicates are: " + string.Join(" ",result));
c# online
Result
type a sentence: number of duplicates: 3 duplicates are: h i s
thanks but I can't use Linq, this is an assignment and we are limited, I have to use a simple code like the above, my issue is that I can't get it to count or list properly.
– Nas
Nov 11 at 5:02
@Nas Ok I edit my answer it can show like your expect result
– D-Shih
Nov 11 at 5:03
add a comment |
up vote
4
down vote
up vote
4
down vote
You can try to use linq instead of for loop.
GroupBy
make a group and use where
get count
greater than 1.
var r= str.Replace(" ", "").GroupBy(_ => _).Where(x => x.Count() > 1).Select(x => x.Key);
then use string.Join
method and linq count
instead of a loop to get your expect result.
Console.Write("type a sentence: ");
String str = Console.ReadLine();
var result = str.Replace(" ", "")
.GroupBy(_ => _)
.Where(x => x.Count() > 1)
.Select(x => x.Key);
Console.WriteLine("number of duplicates: " + result.Count() + "r" + "duplicates are: " + string.Join(" ",result));
c# online
Result
type a sentence: number of duplicates: 3 duplicates are: h i s
You can try to use linq instead of for loop.
GroupBy
make a group and use where
get count
greater than 1.
var r= str.Replace(" ", "").GroupBy(_ => _).Where(x => x.Count() > 1).Select(x => x.Key);
then use string.Join
method and linq count
instead of a loop to get your expect result.
Console.Write("type a sentence: ");
String str = Console.ReadLine();
var result = str.Replace(" ", "")
.GroupBy(_ => _)
.Where(x => x.Count() > 1)
.Select(x => x.Key);
Console.WriteLine("number of duplicates: " + result.Count() + "r" + "duplicates are: " + string.Join(" ",result));
c# online
Result
type a sentence: number of duplicates: 3 duplicates are: h i s
edited Nov 11 at 5:03
answered Nov 11 at 4:56
D-Shih
24.4k61431
24.4k61431
thanks but I can't use Linq, this is an assignment and we are limited, I have to use a simple code like the above, my issue is that I can't get it to count or list properly.
– Nas
Nov 11 at 5:02
@Nas Ok I edit my answer it can show like your expect result
– D-Shih
Nov 11 at 5:03
add a comment |
thanks but I can't use Linq, this is an assignment and we are limited, I have to use a simple code like the above, my issue is that I can't get it to count or list properly.
– Nas
Nov 11 at 5:02
@Nas Ok I edit my answer it can show like your expect result
– D-Shih
Nov 11 at 5:03
thanks but I can't use Linq, this is an assignment and we are limited, I have to use a simple code like the above, my issue is that I can't get it to count or list properly.
– Nas
Nov 11 at 5:02
thanks but I can't use Linq, this is an assignment and we are limited, I have to use a simple code like the above, my issue is that I can't get it to count or list properly.
– Nas
Nov 11 at 5:02
@Nas Ok I edit my answer it can show like your expect result
– D-Shih
Nov 11 at 5:03
@Nas Ok I edit my answer it can show like your expect result
– D-Shih
Nov 11 at 5:03
add a comment |
up vote
0
down vote
Here is another solution without linq:
static void Main(string args)
string longText = @"your sentence comes here";
foreach (var character in CharacterCount.Count(longText))
if(character.Value>1)
Console.WriteLine("0 - 1", character.Key, character.Value);
class CharacterCount
public static SortedDictionary<char, ulong> Count(string stringToCount)
SortedDictionary<char, ulong> characterCount = new SortedDictionary<char, ulong>();
foreach (var character in stringToCount)
if (!characterCount.ContainsKey(character))
characterCount.Add(character, 1);
else
characterCount[character]++;
return characterCount;
Consider usingTryGetValue
rather thanContainsKey
.
– mjwills
Nov 11 at 8:53
add a comment |
up vote
0
down vote
Here is another solution without linq:
static void Main(string args)
string longText = @"your sentence comes here";
foreach (var character in CharacterCount.Count(longText))
if(character.Value>1)
Console.WriteLine("0 - 1", character.Key, character.Value);
class CharacterCount
public static SortedDictionary<char, ulong> Count(string stringToCount)
SortedDictionary<char, ulong> characterCount = new SortedDictionary<char, ulong>();
foreach (var character in stringToCount)
if (!characterCount.ContainsKey(character))
characterCount.Add(character, 1);
else
characterCount[character]++;
return characterCount;
Consider usingTryGetValue
rather thanContainsKey
.
– mjwills
Nov 11 at 8:53
add a comment |
up vote
0
down vote
up vote
0
down vote
Here is another solution without linq:
static void Main(string args)
string longText = @"your sentence comes here";
foreach (var character in CharacterCount.Count(longText))
if(character.Value>1)
Console.WriteLine("0 - 1", character.Key, character.Value);
class CharacterCount
public static SortedDictionary<char, ulong> Count(string stringToCount)
SortedDictionary<char, ulong> characterCount = new SortedDictionary<char, ulong>();
foreach (var character in stringToCount)
if (!characterCount.ContainsKey(character))
characterCount.Add(character, 1);
else
characterCount[character]++;
return characterCount;
Here is another solution without linq:
static void Main(string args)
string longText = @"your sentence comes here";
foreach (var character in CharacterCount.Count(longText))
if(character.Value>1)
Console.WriteLine("0 - 1", character.Key, character.Value);
class CharacterCount
public static SortedDictionary<char, ulong> Count(string stringToCount)
SortedDictionary<char, ulong> characterCount = new SortedDictionary<char, ulong>();
foreach (var character in stringToCount)
if (!characterCount.ContainsKey(character))
characterCount.Add(character, 1);
else
characterCount[character]++;
return characterCount;
answered Nov 11 at 5:06
Eray Balkanli
3,85741943
3,85741943
Consider usingTryGetValue
rather thanContainsKey
.
– mjwills
Nov 11 at 8:53
add a comment |
Consider usingTryGetValue
rather thanContainsKey
.
– mjwills
Nov 11 at 8:53
Consider using
TryGetValue
rather than ContainsKey
.– mjwills
Nov 11 at 8:53
Consider using
TryGetValue
rather than ContainsKey
.– mjwills
Nov 11 at 8:53
add a comment |
up vote
0
down vote
This may help.
Console.Write("type a sentence: ");
String str = Console.ReadLine();
char arr = str.ToCharArray();
int count = new int[10000];
for (int j = 0; j < arr.Length; j++)
int ans=0;
for (int j = 0; j < count.Length; j++)
if(count[j]>1)
ans++;
Console.Write("Number of duplicates: " + ans +" duplicates are: ");
for (int j = 0; j < count.Length; j++)
if(count[j]>1)
Console.Write((char)j +" " );
Console.WriteLine();
}
Thanks Chameera, it works, just couple of notes, it is taking the Space as a character (for example "this is my house" is showing as 4 duplicates instead of 3), how can we avoid that. and also is it possible to list the duplicates on the same line instead of each on a separate line?
– Nas
Nov 11 at 5:40
@Nas Hope this will solve your problem
– Chameera
Nov 16 at 11:51
add a comment |
up vote
0
down vote
This may help.
Console.Write("type a sentence: ");
String str = Console.ReadLine();
char arr = str.ToCharArray();
int count = new int[10000];
for (int j = 0; j < arr.Length; j++)
int ans=0;
for (int j = 0; j < count.Length; j++)
if(count[j]>1)
ans++;
Console.Write("Number of duplicates: " + ans +" duplicates are: ");
for (int j = 0; j < count.Length; j++)
if(count[j]>1)
Console.Write((char)j +" " );
Console.WriteLine();
}
Thanks Chameera, it works, just couple of notes, it is taking the Space as a character (for example "this is my house" is showing as 4 duplicates instead of 3), how can we avoid that. and also is it possible to list the duplicates on the same line instead of each on a separate line?
– Nas
Nov 11 at 5:40
@Nas Hope this will solve your problem
– Chameera
Nov 16 at 11:51
add a comment |
up vote
0
down vote
up vote
0
down vote
This may help.
Console.Write("type a sentence: ");
String str = Console.ReadLine();
char arr = str.ToCharArray();
int count = new int[10000];
for (int j = 0; j < arr.Length; j++)
int ans=0;
for (int j = 0; j < count.Length; j++)
if(count[j]>1)
ans++;
Console.Write("Number of duplicates: " + ans +" duplicates are: ");
for (int j = 0; j < count.Length; j++)
if(count[j]>1)
Console.Write((char)j +" " );
Console.WriteLine();
}
This may help.
Console.Write("type a sentence: ");
String str = Console.ReadLine();
char arr = str.ToCharArray();
int count = new int[10000];
for (int j = 0; j < arr.Length; j++)
int ans=0;
for (int j = 0; j < count.Length; j++)
if(count[j]>1)
ans++;
Console.Write("Number of duplicates: " + ans +" duplicates are: ");
for (int j = 0; j < count.Length; j++)
if(count[j]>1)
Console.Write((char)j +" " );
Console.WriteLine();
}
edited Nov 16 at 11:51
answered Nov 11 at 5:15
Chameera
548
548
Thanks Chameera, it works, just couple of notes, it is taking the Space as a character (for example "this is my house" is showing as 4 duplicates instead of 3), how can we avoid that. and also is it possible to list the duplicates on the same line instead of each on a separate line?
– Nas
Nov 11 at 5:40
@Nas Hope this will solve your problem
– Chameera
Nov 16 at 11:51
add a comment |
Thanks Chameera, it works, just couple of notes, it is taking the Space as a character (for example "this is my house" is showing as 4 duplicates instead of 3), how can we avoid that. and also is it possible to list the duplicates on the same line instead of each on a separate line?
– Nas
Nov 11 at 5:40
@Nas Hope this will solve your problem
– Chameera
Nov 16 at 11:51
Thanks Chameera, it works, just couple of notes, it is taking the Space as a character (for example "this is my house" is showing as 4 duplicates instead of 3), how can we avoid that. and also is it possible to list the duplicates on the same line instead of each on a separate line?
– Nas
Nov 11 at 5:40
Thanks Chameera, it works, just couple of notes, it is taking the Space as a character (for example "this is my house" is showing as 4 duplicates instead of 3), how can we avoid that. and also is it possible to list the duplicates on the same line instead of each on a separate line?
– Nas
Nov 11 at 5:40
@Nas Hope this will solve your problem
– Chameera
Nov 16 at 11:51
@Nas Hope this will solve your problem
– Chameera
Nov 16 at 11:51
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245955%2fc-sharp-find-the-count-of-duplicate-characters-in-an-array-and-list-them%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
github.com/morelinq/MoreLINQ/issues/125 may be of interest.
– mjwills
Nov 11 at 6:30