Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

i want to check if the user inputs a same string twice using array. don't have any idea on what codes to use can you suggest anything?

if the user inputs "1A" twice i want to print "Already Taken", and if the user enter a string which is not in my Array(arr) i want to print "invalid input"

this is my array

      string[,] arr = new string[,]
        {
            {"1A","2A","3A","4A","5A"},
            {"1B","2B","3B","4B","5B"},
            {"1C","2C","3C","4C","5C"},
            {"1D","2D","3D","4D","5D"},
        };
share|improve this question
    
linq will be a good start –  Dor Cohen 11 hours ago
    
You'll have to give some sample input/output, the question is not very clear –  Jonesy 11 hours ago
2  
@DorCohen that really doesn't sound like a LINQ question; I genuinely have no clue what the OP is trying to do, but I'm still pretty confident that LINQ isn't the first tool to pick up –  Marc Gravell 11 hours ago
    
@Josh you haven't given us any context as to how the array relates to the user's input... is the question here "the user must select two different values that are in the array"?. Or is it "the array represents the user's input; the values must be distinct"? –  Marc Gravell 11 hours ago
    
@MarcGravell take a look on my answer, I think that's what he meant –  Dor Cohen 11 hours ago

4 Answers 4

up vote 3 down vote accepted

You can use a HashSet<string> to check if there are duplicates:

var set = new HashSet<string>();
bool noDuplicate = arr.Cast<string>().All(set.Add);
share|improve this answer
1  
very tidy implementation –  Marc Gravell 11 hours ago

You can use Set, e.g. HashSet<String>:

  HashSet<String> hs = new HashSet<string>();

  foreach(var item in arr)
    if (!hs.Add(item)) {
      // User used "item" at least twice
      break; 
    }
share|improve this answer
2  
btw; you can simplify to just if(!hs.Add(item)) { /* fail */ } - this avoids doing a separate Contains/Add test per item. –  Marc Gravell 11 hours ago
1  
@Marc Gravell: thank you for the suggestion! –  Dmitry Bychenko 11 hours ago

You can try using LINQ, something like this:

var query = arr.GroupBy(x=>x)
              .Where(g=>g.Count()>1)
              .ToList();
share|improve this answer

You can try :

  HashSet<String> hash = new HashSet<string>();

  string input = "TEST";
  bool found = false;

  foreach(string item in arr)
  {
      if (item.Equals(input))
      {
          if (hash.Contains(item))
          {
              Console.WriteLine("Already Taken");
          }
          else
          {
              hash.Add(item);
          }
          found = true;
          break;
      }
   }

  if (!found)
  {
      Console.WriteLine("Invalid Input");
  }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.