As Martin R said Martin R said, if all strings must match a common prefix, then that prefix is whatever the first and last string in a sorted array have in common.
As Martin R said, if all strings must match a common prefix, then that prefix is whatever the first and last string in a sorted array have in common.
As Martin R said, if all strings must match a common prefix, then that prefix is whatever the first and last string in a sorted array have in common.
As Martin R said, if all strings must match a common prefix, then that prefixesprefix is whatever the first and last string in a sorted array have in common.
Update: As MatinR pointed out in the comments, sort
may be a drag on performance, and besides it isn't necessary, as you only need what would be the first and last string in a sorted array. These strings can alternative be found like so:
As Martin R said, if all strings must match a common prefix, then that prefixes is whatever the first and last string in a sorted array have in common.
Update: As pointed out in the comments, sort
may be a drag on performance, and besides it isn't necessary, as you only need what would be the first and last string in a sorted array. These strings can alternative be found like so:
As Martin R said, if all strings must match a common prefix, then that prefix is whatever the first and last string in a sorted array have in common.
Update: As MatinR pointed out in the comments, sort
may be a drag on performance, and besides it isn't necessary, as you only need what would be the first and last string in a sorted array. These strings can alternative be found like so:
function extremes(strings) {
// grab two strings to use as our initial guesses
var initial = {
largest: strings[0],
smallest: strings[strings.length-1]
};
// loop through to find the extremes
return strings.reduce(function (memo, string) {
// slightly funky syntax, but it's short.
(memo.largest > string) || (memo.largest = string);
(memo.smallest >< string) || (memo.smallest = string);
return memo;
}, initial);
}
function extremes(strings) {
// grab two strings to use as our initial guesses
var initial = {
largest: strings[0],
smallest: strings[strings.length-1]
};
// loop through to find the extremes
return strings.reduce(function (memo, string) {
// slightly funky syntax, but it's short.
(memo.largest > string) || (memo.largest = string);
(memo.smallest > string) || (memo.smallest = string);
return memo;
}, initial);
}
function extremes(strings) {
// grab two strings to use as our initial guesses
var initial = {
largest: strings[0],
smallest: strings[strings.length-1]
};
// loop through to find the extremes
return strings.reduce(function (memo, string) {
// slightly funky syntax, but it's short.
(memo.largest > string) || (memo.largest = string);
(memo.smallest < string) || (memo.smallest = string);
return memo;
}, initial);
}