I am trying to remove a duplicate letter of first or last letter, with an exception of containing 'a' as first letter and 'l' and 'e' as last letter. However, this code does not look efficient to me because, what if I had 20 letters of exception? Could I put this in a string or an array? I would not want to compare each letter all in one if statement. What would be a better way to rewrite this code? Example of what the code should do:
- aaron should return aaron
- bill should return bill
- lee should return lee
All other names with duplicate first and last letters should return with the removed duplicate letter. This is a unit test, where firstName is being passed as a string. Instead of using if statement of each letter to check, is there an alternative solution?
UnitTest:
[TestCase("aaron", "aaron")]
public void QualityAssurance(string firstName, string expected)
{
var actual = warmups.QualityAssurance(firstName);
Assert.AreEqual(expected, actual);
}
Unit:
public string QualityAssurance(string firstName)
{
if (firstName[0] != 'a' && firstName[firstName.Length - 1] != 'l' && firstName[firstName.Length - 1] != 'e')
{
if (firstName[0] == firstName[1] && firstName[firstName.Length - 1] == firstName[firstName.Length - 2])
{
return firstName.Remove(0, 1).Remove(firstName.Length - 2, 1);
}
else if (firstName[0] == firstName[1])
{
return firstName.Remove(0, 1);
}
else if (firstName[firstName.Length - 1] == firstName[firstName.Length - 2])
{
return firstName.Remove(firstName.Length - 1);
}
}
return firstName;
}