Problem overview
GetFullNames(string[] names) receives an array in which every first name is immediately followed by its last name. It returns a List<string> with one full name per pair, joined by a single space.
For ["John", "Doe", "Andy", "Smith", "Mary", "Johnson"] the result is ["John Doe", "Andy Smith", "Mary Johnson"]. Six input strings make three output strings, so the result is half as long as the input.
Approach
The first names sit at the even indices 0, 2, 4, ... and each last name sits right after its first name. A loop that steps through the array two positions at a time lands on a first name on every pass, and names[i + 1] is its partner. Each pass builds one full name and stores it.
The output size isn't known up front in a way that fits a fixed array neatly, and the return type is a List<string> anyway. A list starts empty and grows with Add, which suits collecting results one at a time.
Step by step
- Create the result list.
new List<string>()is an empty, growable list of strings, ready to be filled. - Loop over the even indices. Writing the update as
i += 2makesitake the values 0, 2, 4, and so on. - Build the full name. Concatenate
names[i], a single space andnames[i + 1]with+. The space is a separate string literal," ". - Add it to the list.
Addappends at the end, so the full names keep the order of the input pairs. - Return after the loop. Returning inside the loop would end the method after the first pair.
The solution in C#
class Solution {
public List<string> GetFullNames(string[] names) {
List<string> answer = new List<string>();
for (int i = 0; i < names.Length; i += 2) {
string name = names[i] + " " + names[i + 1];
answer.Add(name);
}
return answer;
}
}
Some C# details:
- Arrays and lists use different size members. An array has a
Lengthproperty, which the loop uses. AList<T>hasCount. The method returns a list, so a caller asks it forCount. List<T>lives inSystem.Collections.Generic. Code that uses it needs that namespace available.- Indexing is zero-based. The first element of
namesisnames[0], and the last valid index isnames.Length - 1. - Strings are immutable. The
+expression creates a brand-new string for each pair and leaves the originals untouched. - The list can be built with the explicit type or with
new().List<string> answer = new();is an equivalent shorter form in modern C#. The longer form above matches the lesson's code.
Complexity
Time: O(n), where n is the length of names. The loop makes n / 2 passes, and each one joins two short strings and adds one item to the list. List.Add takes constant time on average, because the list doubles its internal capacity when it fills up.
Space: O(n). The result holds n / 2 strings, and each is about as long as two input names.
Edge cases and common mistakes
- Empty array. The loop doesn't run and an empty list comes back.
- Odd length. If the array had an unpaired last name, say three entries, then
names[i + 1]on the last pass would be out of range and throw anIndexOutOfRangeException. The task promises pairs, so the solution doesn't check for this. - Stepping by one. Using
i++would build "John Doe", then "Doe Andy", and so on, giving twice as many names, most of them wrong. - Forgetting the space.
names[i] + names[i + 1]gives"JohnDoe". The task asks for exactly one space between the names. - Returning inside the loop. Only the first full name would be returned.
- Using the wrong index for the last name.
names[i - 1]pairs a name with the one before it, which is a first name wheniis a last-name index. Becauseionly lands on first names, the partner is alwaysi + 1.
Testing
["John", "Doe", "Andy", "Smith", "Mary", "Johnson"]gives["John Doe", "Andy Smith", "Mary Johnson"].["Ada", "Lovelace"]gives["Ada Lovelace"].[]gives[].