Home > Blockchain >  I want to find how many times a name used in my vektor
I want to find how many times a name used in my vektor

Time:11-04

            Console.WriteLine("Here you can write the name as many as you want,
            and if u wanna end up just write No!");

            Console.WriteLine("\n");
            Console.WriteLine("Start writing a name:");

            
            string[] namnArray = new string[200];
            for (int i = 0; i < namnArray.Length; i  )
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                namnArray[i] = Console.ReadLine();
                Console.ForegroundColor = ConsoleColor.White;
                Console.Write("Do u wanna write another one?  ");

                if (namnArray[i] == "No")
                {
                    Console.WriteLine("\n");
                    Console.ForegroundColor = ConsoleColor.Magenta;
                    Console.WriteLine("write a name of your choice:");
                    
                    //Here i wanna know how many times a names used as the user wrote up there!
                    but dont know how to do it, if you wanna help me will be thankful!



                }

            }

I tried to make a new string variable as

string youChoice = Console.ReadLine();

and then I don't know how to go further!

CodePudding user response:

I wouldn't use a fixed size array for this. You're almost certainly better off using List<string> instead.

var namnList = new List<string>();
string enteredNamn;
do {
    ...
    enteredNamn = Console.ReadLine();
    namnList.Add(enteredNamn);

} while (enteredNamn != "No");

Then you can use the List<>.Count() to count all matches.

var chosenNamn = Console.ReadLine();
var count = namnList.Count(x => x == chosenNamn);

Disclaimer - code for illustrative purposes only, I make no guarantee that this will even compile

CodePudding user response:

Console.WriteLine("Here you can write the name as many as you want, and if u wanna end up just write No!");

  Console.WriteLine("\n");
  Console.WriteLine("Start writing a name:");
  string[] namnArray = new string[200];
 
  for (int i = 0; i < namnArray.Length; i  )
  {
    Console.ForegroundColor = ConsoleColor.Yellow;
    namnArray[i] = Console.ReadLine();
    Console.ForegroundColor = ConsoleColor.White;
    Console.Write("Do u wanna write another one?  ");

    if (namnArray[i] == "No")
    {
      Console.WriteLine("\n");
      Console.ForegroundColor = ConsoleColor.Magenta;
      Console.WriteLine("write a name of your choice:");
      var names = string.Join("", namnArray);
      var input = Console.ReadLine();
      var count = Regex.Matches(names, input).Count;
      Console.WriteLine("The number of ocurance is "   count);

    }

CodePudding user response:

General Kenobi. You are a bold one.

You can use Array.FindAll:

var number = Array.FindAll(namnArray, x => x == name ).Count();

The FindAll

Retrieves all the elements that match the conditions defined by the specified predicate.

For more info about FindAll and example of use follow the link to the official documentation.

  •  Tags:  
  • c#
  • Related