Java Interview Practice Problem (Beginner): Concatenate Characters

  • Post last modified:August 31, 2023
  • Reading time:1 mins read

Problem Statement

We have been given a List of characters that contains each character of the author’s name. Our task is to concatenate them and return the author name as String.

Example

author chars = { 'S', 'A', 'M' }
output => "SAM"

Before jumping to a solution consider giving an attempt

Solution

StringBuilder sb = new StringBuilder();
chars.forEach(sb::append);
sb.toString();

List<Character> authorChars = Arrays.asList('S', 'A', 'M');
String authorName = chars
     .stream()
     .map(a -> "" + a)
     .collect(Collectors.joining());

Conclusion

Leave a Reply