If I were an interview, and you solved it only for entering numbers, my next question would be: "How would you solve this problem for non-numeric input?" because I would not be looking for mathematical tricks. Instead, how about this?
List<String> options = new ArrayList<>(Arrays.asList("bob", "fred")); options.remove("bob"); System.out.println(options.get(0));
This, obviously, easily adapts to any type, including Object , as long as the equality of objects works correctly and as a bonus it can be expressed much more concisely in other languages ββsuch as Groovy:
println((["bob", "fred"] - "bob").first())
The output, in any case, is clearly "fred". If I were one of the interviews, this is the answer I would be looking for.
Ryan Stewart Feb 24 '13 at 9:15 2013-02-24 21:15
source share