0

I need to loop through an array of strings and know if I've seen a particular string value before. Normally, I would have written something like this in other languages:

String oldValue="";
String newValue;
for (i=0;i<myarray.Length;i++)
{
    newValue=myarray[i];
    if (oldValue==newValue)
        break;
    ...

    oldValue=newValue;

}

However, this doesn't work in C# as Strings are immutable. It looks like I could do this with a Regular Expression if I just replaced the entire string, but that seems like extra overhead. How have other people handled this before?

Thank you

5
  • 4
    Strings are immutable, but you can still assign variables to new values. Can you post a complete code sample demonstrating the problem that you're having? (if you have, refer to Grant's comment above; where have you assigned newValue? Obviously this isn't a full program; it wouldn't compile as-is.) Commented Mar 2, 2014 at 2:05
  • It doesn't appear as though you've actually attempted this Commented Mar 2, 2014 at 2:07
  • Sorry guys I missed setting the newValue. I've edited it to include. Commented Mar 2, 2014 at 2:33
  • Hi Trebor -- I think you misunderstand what immutable strings in .net means. You can still assign a new value to a string variable -- the variable is not read only, or initialize once. The immutability just means that 1. Every time you assign a new value to the string variable, a new string is allocated behind the scenes, and 2. You can't change just one character of a string, like newValue[2] = 'g'; Commented Mar 2, 2014 at 2:52
  • Chris and JMarsch, you each hit it on the head with what I wasn't understanding. I had encountered the immutability issue in a previous task and came away thinking they were unchangeable and that you couldn't assign a new value to them. So I wrote out a demo just to try and prove what I thought to be true, and found out I was completely wrong about how this actually worked. Thank you for the clarification! Commented Mar 2, 2014 at 2:57

2 Answers 2

5

I'm not sure whether I understood your question, but if what you intend is to detect the first string that is repeated in the array, you need to remember all of them. I suggest using a HashSet, so at least it runs in O(n), like this:

HashSet<string> prevSet = new HashSet<string>();

foreach ( string str in myArray )
  if ( !prevSet.Add(str) ) return str;
Sign up to request clarification or add additional context in comments.

2 Comments

+1. You can skip Contains and just do if (!prevSet.Add(str)) return str; See HashSet.Add
Danyluis, thank you! You are correct that I need to really remember all of the previous values and this is a much better solution than what I was thinking of doing.
1

You could do this to make a list of word frequencies:

var frequency =
    myarray
        .GroupBy(x => x)
        .Select(x => new
        {
            Value = x.Key,
            Count = x.Count(),
        });

You could then just filter this list where Count > 1.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.