Click here to Skip to main content
14,696,278 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Basically i have a char array and i want to be able to count the number of duplicates from it.

Edit
Basically i was trying to make the terminal game from fallout. A pass-code will be entered and people will try to guess it. When guessing you will be told how many of these letters are correct and whether the length of the word is correct.

I use this to convert the actual word to an array
char[] ActualChar = new char[WordLenght];

            while (ApproachingLenght < WordLenght) 
            {
                foreach (char Letter in Word)
                { 
                    ActualChar[ApproachingLenght] = Letter;
                    ApproachingLenght++;
                }
            }

Guess into array
char[] GuessChar = new char[GuessLenght];
            while (ApproachingGuessLenght < GuessLenght)
            {
                foreach (char Letter in Guess)
                {
                    GuessChar[ApproachingGuessLenght] = Letter;
                    ApproachingGuessLenght++;
                }
            }

This to check for similarities between the two arrays, HOWEVER i get a problem here basically if the actual word is "Hello" and guess "Helli" it will say 6 characters are similar rather than 4. I found the problem to be the 2 l's as "abcd, asdf" did not have this issue.
if(GuessLenght == WordLenght)
            {
                //Check Each Element


                for (int i = 0; i < WordLenght; i++)
                {
                    for(int p = 0; p < WordLenght; p++)
                    {
                        if (GuessChar[i] == ActualChar[p])
                        {
                            LetterGuessed++;
                        }
                    }
                }
                
            }
            else
            {
                if(GuessLenght> WordLenght)
                {
                    Console.WriteLine("Wrong Lenght, Actual Lenght is : Shorter");
                }
                else
                {
                    Console.WriteLine("Wrong Lenght, Actual Lenght is : Longer");
                }
            }


Therefore i planned to find the number of duplicates in the array and minus it from the letters guessed.

What I have tried:

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

This i found online and just added it.
Posted
Updated 13-May-20 0:27am
v3
Comments
Afzaal Ahmad Zeeshan 12-May-20 18:12pm
   
What is this CharacterCount.Count method?
PIEBALDconsult 12-May-20 18:24pm
   
Unclear.
How many characters are duplicated?
Or how many total duplicate characters there are?
Please provide sample input and output.
PIEBALDconsult 13-May-20 19:00pm
   
Having seen your edit... Have a re-think. You needn't focus on duplicates.
But I still don't understand the goal, all I know is that F("Hello","Helli") is _not_ 6.

var uniqueChars = longText.Distinct();

foreach (var ch in uniqueChars)
{
    Console.WriteLine(
        $"char: {ch} char ascii value: {(int)ch} instances: {longText.Count(c => c == ch)}\r\n");
}

Console.WriteLine($"nduplicates: {longText.Count() - uniqueChars.Count()}");
character.Key and character.Value are not C#.
   
v2
Comments
Maciej Los 13-May-20 2:39am
   
5ed!
var duplicateDict = new Dictionary<char, int>();
		string longText = @"your sentence comes here";
		foreach (var ch in longText)
		{
			if (duplicateDict.ContainsKey(ch))
			{
				duplicateDict[ch]++;
			}
			else
			{
				duplicateDict.Add(ch, 1);
			}
		}

		foreach (var ch in duplicateDict.Where(x => x.Value > 1))
		{
			Console.WriteLine(string.Format("Duplicate Character {0}, Count {1}", ch.Key, ch.Value));
		}
   
Comments
PIEBALDconsult 12-May-20 20:24pm
   
Best not to do peoples' homework for them.
And that can be improved.
Maciej Los 13-May-20 2:40am
   
Dictionary is good idea!
5ed!
NotAComputerScienceStudent 13-May-20 4:59am
   
isn't homework i have nothing better to do in quarantine, i'm a mechanics student.
Maciej Los 13-May-20 5:40am
   
Sorry, but you have replied to wrong person...
BTW: Have ou seen my solution?
If you would like to get only duplicates...

string longText = @"your sentence comes here";
Dictionary<char, int> result = longText
	.GroupBy(x=>x)
	.Select(grp => new KeyValuePair<char, int>(grp.Key, grp.Count()))
	.Where(kvp=> kvp.Value>1)
	.OrderByDescending(kvp=> kvp.Value)
	.ToDictionary(kvp=> kvp.Key, kvp=>kvp.Value);
	
foreach(KeyValuePair<char, int> kvp in result)
	Console.WriteLine($"'{kvp.Key}' => {kvp.Value}" );
   
Comments
Maciej Los 13-May-20 5:52am
   
You mean namespace: System.Linq
See: Enumerable.GroupBy Method (System.Linq) | Microsoft Docs[^]
NotAComputerScienceStudent 13-May-20 5:53am
   
It worked
Maciej Los 13-May-20 5:54am
   
I'm glad.
Does it solves your issue?
Well, if you do it this way, you'll get kicked out of class:

System.Text.RegularExpressions.Regex reg =
  new System.Text.RegularExpressions.Regex
  (
    @"(?i)^
    ((?'A'A)|(?'B'B)|(?'C'C)|(?'D'D)|(?'E'E)
    |(?'F'F)|(?'G'G)|(?'H'H)|(?'I'I)|(?'J'J)
    |(?'K'K)|(?'L'L)|(?'M'M)|(?'N'N)|(?'O'O)
    |(?'P'P)|(?'Q'Q)|(?'R'R)|(?'S'S)|(?'T'T)
    |(?'U'U)|(?'V'V)|(?'W'W)|(?'X'X)|(?'Y'Y)
    |(?'Z'Z)|(?'Other'.))*$"
  ,
    System.Text.RegularExpressions.RegexOptions.Compiled
    |
    System.Text.RegularExpressions.RegexOptions.ExplicitCapture
    |
    System.Text.RegularExpressions.RegexOptions.IgnorePatternWhitespace
  ) ;

string[] n = reg.GetGroupNames() ;

System.Text.RegularExpressions.MatchCollection mat =
  reg.Matches ( "The quick brown fox jumps over the lazy dog." ) ;

for ( int i = 0 ; i < mat.Count ; i++ )
{
  for ( int g = 1 ; g < n.Length ; g++ )
  {
    System.Console.WriteLine ( "{0} {1}" , n [ g ] , mat [ i ] .Groups [ n [ g ] ].Captures.Count ) ;
  }
}
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900