Home > Net >  Detect the same text inside string array in c#
Detect the same text inside string array in c#

Time:11-26

I am trying to find a way to detect if there is/are the same text in array input, all five of the Name input must be different, and if there is any similarity, it will automatically request input again.

Example:

Name 1: Max
Address 1: address a

Name 2: Max
Address 2: address b

You can see that both of the name input is similar, i want it to Console.WriteLine("The name is already added, please add a different name); and request input for Name 2 again.

Just like this:

Name 1: Max
Address 1: address a

Name 2: Max
Address 2: address b

Name is already added, Please input a different name!`
Name 2: Minimum

Here is my code so far:

    var a = 5;
    string[,] input = new string[a, 2];

    for (a = 0; a < 5; a  )
    {
        Console.Write($"Name {a   1} : ");
        input[a, 0] = Console.ReadLine();
        input[a, 0] = input[a, 0].ToLower();
        Console.Write($"Address {a   1} : ");
        input[a, 1] = Console.ReadLine();
        
    }

NOTE: This is between five inputs

CodePudding user response:

You can use HashSet<string> to store unique strings (names):

  //DONE: it's unclear what "a" name means, let it be "count"
  var count = 5;
  //TODO: I've kept 2d array from the question, 
  // a class (tuple) will be better here 
  string[,] input = new string[count, 2];
  
  var takenNames = new HashSet<string>(StringComparer.OrdinalIgnoreCase);

  //DONE: don't use magic constants - 5
  for (int i = 0; i < input.GetLength(0);   i) {
    // Keep asking user until unique name is provided:
    while (true) {
      Console.Write($"Name {i   1} : ");
      input[i, 0] = Console.ReadLine();

      // stop asking if and only if the name is unique  
      if (takenNames.Add(input[i, 0]))
        break;

      Console.WriteLine("Name is already added, Please input a different name!");
    }   

    Console.Write($"Address {i   1} : ");
    input[i, 1] = Console.ReadLine();       
  }

Edit: As Dai mentioned in the comments, an array of custom classes, named tuples, records etc. is better design then 2d array of strings, e.g.

  var count = 5;
  // let it be readable: named tuple with name and address properties  
  var input = new (string name, string address)[count];

  var takenNames = new HashSet<string>(StringComparer.OrdinalIgnoreCase);

  for (int i = 0; i < input.GetLength(0);   i) {
    // Keep asking user until unique name is provided:
    while (true) {
      Console.Write($"Name {i   1} : ");
      input[i].name = Console.ReadLine();

      // stop asking if and only if the name is unique  
      if (takenNames.Add(input[i].name))
        break;

      Console.WriteLine("Name is already added, Please input a different name!");
    }

    Console.Write($"Address {i   1} : ");
    input[i].address = Console.ReadLine();
  }

CodePudding user response:

Here is a basic example. You can cut and paste into Linqpad to try it

EDIT Corrected to take into account @Dai comment about the Dictionary constructor overload that takes an IEqualityComparer instance as argument. See the doc about this constructor, and the doc about the StringComparer.

void Main()
{
    var personCount = 5;
    var persons = new Dictionary<string, Person>(StringComparer.OrdinalIgnoreCase);
    
    for (int i = 0; i < personCount; i  )
    {
        var newPerson = new Person();
        Console.WriteLine("Enter a name");
        var name = Console.ReadLine();
        while(persons.ContainsKey(name))
        {
            Console.WriteLine("This name already exists, please enter a new name");
            name = Console.ReadLine();
        }
        newPerson.Name = name;
        Console.WriteLine("Enter an address");
        newPerson.Address = Console.ReadLine();
        persons.Add(name, newPerson);
    }
}

// You can define other methods, fields, classes and namespaces here

public class Person
{
    public string Name { get; set; }
    public string Address { get; set; }
}
  •  Tags:  
  • c#
  • Related