Compare a list of strings: Difference between revisions

Content added Content deleted
(→‎{{header|C++}}: document undefined behavior)
(→‎{{header|Go}}: optimization is the job of the compiler, not of the human)
Line 1,132: Line 1,132:


func AllEqual(strings []string) bool {
func AllEqual(strings []string) bool {
for _, s := range strings {
if len(strings) < 2 {
if s != strings[0] {
return true
}

first := strings[0]
for _, s := range strings[1:] {
if s != first {
return false
return false
}
}
Line 1,146: Line 1,141:


func AllLessThan(strings []string) bool {
func AllLessThan(strings []string) bool {
if len(strings) < 2 {
for i := 1; i < len(strings); i++ {
if !(strings[i - 1] < s) {
return true
}

last := strings[0]
for _, s := range strings[1:] {
if !(last < s) {
return false
return false
}
}
last = s
}
}
return true
return true