engineering recuitment RPSC Programmer Mock Test 2024 Programming and Data Structure Programming in C Function Recursion
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.