The following C# code is supposed to remove all duplicate characters from a string. However, there is an error in the code. Which of the following options correctly identifies and fixes the error?
public string RemoveDuplicates(string str)
{
    string result = "";
    foreach (char c in str)
    {
        if (!result.Contains(c))
        {
            result += c;
        }
    }
    return result;
}

1
Change string result = "" to string result = null.
2
Change !result.Contains(c) to result.Contains(c).
3
Change foreach (char c in str) to for (int i = 0; i < str.Length; i++).
4
Change result += c to result = c.

Sponsored

hivanix.in

Visit

This quiz is brought to you by hivanix.in

🌐 Web App Development

Quick Navigation