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();
















share|improve this question





















  • github.com/morelinq/MoreLINQ/issues/125 may be of interest.
    – mjwills
    Nov 11 at 6:30















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();
















share|improve this question





















  • github.com/morelinq/MoreLINQ/issues/125 may be of interest.
    – mjwills
    Nov 11 at 6:30













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();
















share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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

















  • 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













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





share|improve this answer






















  • 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

















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;







share|improve this answer




















  • Consider using TryGetValue rather than ContainsKey.
    – mjwills
    Nov 11 at 8:53

















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();
}





share|improve this answer






















  • 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











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















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

























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





share|improve this answer






















  • 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














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





share|improve this answer






















  • 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












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





share|improve this answer














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






share|improve this answer














share|improve this answer



share|improve this answer








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
















  • 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












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;







share|improve this answer




















  • Consider using TryGetValue rather than ContainsKey.
    – mjwills
    Nov 11 at 8:53














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;







share|improve this answer




















  • Consider using TryGetValue rather than ContainsKey.
    – mjwills
    Nov 11 at 8:53












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;







share|improve this answer












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;








share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 at 5:06









Eray Balkanli

3,85741943




3,85741943











  • Consider using TryGetValue rather than ContainsKey.
    – 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




Consider using TryGetValue rather than ContainsKey.
– mjwills
Nov 11 at 8:53










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();
}





share|improve this answer






















  • 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















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();
}





share|improve this answer






















  • 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













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();
}





share|improve this answer














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();
}






share|improve this answer














share|improve this answer



share|improve this answer








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

















  • 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


















 

draft saved


draft discarded















































 


draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3

Museum of Modern and Contemporary Art of Trento and Rovereto