An answer is meant to provide a solution to a problem. The trick is identifying the actual problem which doesn't necessarily mean that the problem is the code; it might very well be the approach.
By providing a better approach to the same underlying problem you will have cleared up the problem and it will probably be very useful for future readers, which are the primary target of your answer anyway.
Even if the person that asked the question decides that he will stick to his approach, your answer is still valid.
It gets tricky when you provide an answer that isn't an improvement but just a different approach. We all know any given coding problem can have many solutions where many of them can be equally correct. By providing alternatives in the answer you will encourage a somewhat opinion based answer spree: people will post whatever approach they prefer.
For this reason it is important to only present alternatives with care. It is my rule of thumb to only present an alternative approach if it is objectively better than the original approach or if it has benefits in certain aspects (readability, maintenance, performance, etc).
It is key that you identify the differences between the approaches and present this in a clear way; this will make it easy for people to interpret why you might have chosen this.
Whether or not having multiple approaches to a problem scenario is a good thing is up for discussion. I believe it enhances the quality of the question in general and will provide a valuable resource for future visitors. Nevertheless the answers should differ enough from eachother to actually warrant multiple answers and they should be of good quality.
From a quick glance at your answer (and the 3 upvotes it received since I opened it) I would wager that your answer has met these conditions and is therefore valid.
Answers are meant to solve the problem, not the code.