Fix moveToFront and its headline

yarcat 2021-02-12 18:48:16 +01:00
parent 4c2589e614
commit bffe6342c2

@ -234,15 +234,15 @@ result := in[:j+1]
fmt.Println(result) // [1 2 3 4] fmt.Println(result) // [1 2 3 4]
``` ```
### Move to front, or append if not present, in place ### Move to front, or prepend if not present, in place if possible.
```go ```go
// moveToFront moves needle to the front of haystack, in place if possible. // moveToFront moves needle to the front of haystack, in place if possible.
func moveToFront(needle string, haystack []string) []string { func moveToFront(needle string, haystack []string) []string {
if len(haystack) == 0 || haystack[0] == needle { if len(haystack) != 0 && haystack[0] == needle {
return haystack return haystack
} }
var prev string prev := needle
for i, elem := range haystack { for i, elem := range haystack {
switch { switch {
case i == 0: case i == 0: