If there is a question that asks
How to do X?
which already has some answers, but then someone asks
What is the optimal way of doing X?
is the question seeking for the optimal way considered a duplicate?
Your question
What's the optimal way of doing 'X'?
Is opinion based and not clear. You have to state what do you mean by optimal in terms of performance, execution time, etc.
Asking it on StackOverflow will generate different varieties of answers and mostly opinion-based.
But,
for doing a certain work, people upvote the most optimal way and the best answers rise to the top eventually. If the most optimal way of doing "X" is answered already on "How to do X?" Then this means that there is no more optimal way of doing "X" then "What is the optimal way of doing X?" is a dupe of "How to do X?"
If you want to ask for an optimal way, you must specify a measure that is to be optimized. Usually that will be a combination of execution speed, code size, memory consumption or other stuff, but it's not clear a priori what is meant by optimal. Without a proper definition such questions must be put on hold as unclear until such a definition is added.
Sometimes even with a given definition of "optimal way", the difference between "How to do X?" and "How to do X in this specific optimal way?" might still be very small, because people even without being asked for explicitly tend to give optimized solutions in answers and especially if the answers to both questions are the same, people might still be tempted to close the latter as a duplicate of the former. If you disagree, it's then very important to argue why the solutions to "How to do X?" might not be optimal in the sense of "How to do X in this specific optimal way?".
As a general guide if you want to ask "How to do X in an optimal way?":
O(N log N)
orO(log N)
. I could also be something like "fastest way".O
notation, would that be a duplicate?O
notation.O(log N)
solution with a constant factor of 10^10000 would not be preferable to aO(N log N)
solution with a factor of 2). If that's the case I would recommend that you try asking about the algorithm on the computerscience SE which might give you better answers than StackOverflow.