Ad
  • Custom User Avatar

    Wouldn't this be an incorrect solution as it removes more than what was requested? (spaces vs all whitespaces)

  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Custom User Avatar

    In order to prevent bugs in more complicated situations like this, you should strive not to learn imperative coding style at all. Almost all languages do that kind of work for you out of the box - in Java it's streams. When you start coding like this, you most certainly will produce bugs.

    In this particular case you already produced a bug: what happens if I call this function with an int array of size 0?

  • Default User Avatar

    I usally do code like this as i learn the language. But i hope to figure out other ways as i learn more of java.
    But what do you mean by "dreaded bugs"? It just looks like a simple "brute force" solution to me.
    (btw your missing a "do" before your "it" in your finall sentence)

  • Custom User Avatar

    This is neither "clever" nor a "best practice". I always wonder who rates these solutions.
    All this is, is straight forward error prone and to be avoided imperative code. Used in production this will eventually lead to all of our dreaded bugs.

    DO NOT IT THIS WAY. Never.

  • Custom User Avatar

    This adds an extra array allocation, because the StringBuilder cannot be presized appropriately.

  • Custom User Avatar

    weird choice. if you do calculation withou loop (unrolled style), what for is array and loop, dah