Get Full Names II in JavaScript (Time Complexity: O(n))


Given an array of strings names that contain first and last names adjacent to each other, return an array containing the full names (separated by one space)

Example:

Input:  names = ["John", "Doe", "Andy", "Smith", "Mary", "Johnson"]

Output:  ["John Doe", "Andy Smith", "Mary Johnson"]

Understanding the Problem

The core challenge of this problem is to pair adjacent elements in the array to form full names. This is a common task in data processing where names might be split into separate fields and need to be combined for display or further processing.

Potential pitfalls include handling arrays with an odd number of elements or empty arrays. However, based on the problem statement, we assume that the input will always have an even number of elements.

Approach

To solve this problem, we can iterate through the array, taking two elements at a time and concatenating them with a space in between. This approach ensures that we efficiently pair the names without unnecessary operations.

Let's break down the steps:

  1. Initialize an empty array to store the full names.
  2. Iterate through the input array with a step of 2.
  3. For each iteration, concatenate the current element and the next element with a space in between.
  4. Push the concatenated string into the result array.
  5. Return the result array.

Algorithm

Here is a step-by-step breakdown of the algorithm:

  1. Initialize an empty array fullNames.
  2. Loop through the names array with a step of 2.
  3. In each iteration, concatenate names[i] and names[i+1] with a space.
  4. Push the concatenated string into fullNames.
  5. After the loop, return fullNames.

Code Implementation

// Function to get full names from an array of first and last names
function getFullNames(names) {
    // Initialize an empty array to store the full names
    const fullNames = [];
    
    // Loop through the names array with a step of 2
    for (let i = 0; i < names.length; i += 2) {
        // Concatenate the current element and the next element with a space
        const fullName = names[i] + ' ' + names[i + 1];
        // Push the concatenated string into the result array
        fullNames.push(fullName);
    }
    
    // Return the result array
    return fullNames;
}

// Example usage:
const names = ["John", "Doe", "Andy", "Smith", "Mary", "Johnson"];
console.log(getFullNames(names)); // Output: ["John Doe", "Andy Smith", "Mary Johnson"]

Complexity Analysis

The time complexity of this approach is O(n), where n is the number of elements in the input array. This is because we are iterating through the array once with a step of 2.

The space complexity is also O(n) because we are storing the full names in a new array.

Edge Cases

Potential edge cases include:

  • An empty array: The function should return an empty array.
  • An array with an odd number of elements: Based on the problem statement, we assume this will not occur.

Example of handling an empty array:

console.log(getFullNames([])); // Output: []

Testing

To test the solution comprehensively, consider the following test cases:

  • Standard case with multiple names: ["John", "Doe", "Andy", "Smith", "Mary", "Johnson"]
  • Empty array: []
  • Array with one pair: ["Alice", "Wonderland"]

Example test cases:

console.log(getFullNames(["John", "Doe", "Andy", "Smith", "Mary", "Johnson"])); // Output: ["John Doe", "Andy Smith", "Mary Johnson"]
console.log(getFullNames([])); // Output: []
console.log(getFullNames(["Alice", "Wonderland"])); // Output: ["Alice Wonderland"]

Thinking and Problem-Solving Tips

When approaching such problems, consider the following tips:

  • Break down the problem into smaller, manageable steps.
  • Think about edge cases and how to handle them.
  • Write pseudo-code before jumping into the actual implementation.
  • Test your solution with a variety of test cases to ensure its robustness.

Conclusion

In this blog post, we discussed how to solve the problem of concatenating adjacent first and last names in an array. We covered the problem definition, approach, algorithm, code implementation, complexity analysis, edge cases, and testing. Understanding and solving such problems is crucial for data processing tasks, and practicing these problems helps improve problem-solving skills.

Additional Resources

For further reading and practice, consider the following resources: