All equal elements

Report a typo

Lena wants to create a generic function allEqual that checks if all the elements in a slice have the same value.

Lena has already created a custom type comparableSlice that can only have comparable data types as slice elements, and has also written the algorithm to check if all the elements in the slice have the same value.

Your task is to help Lena write the correct generic function declaration for allEqual, using the custom comparableSlice type she created.

Sample Input 1:

𐐘 ඞ ඞ
1 1 1

Sample Output 1:

false true
Write a program in Go
package main

import "fmt"

// DO NOT delete the custom type 'comparableSlice'
// It will be required as part of the allEqual generic function declaration!
type comparableSlice[T comparable] []T

func allEqual[? ?](? ?[?]) ? {
if len(s) == 0 {
return true
}
last := s[0]
for _, cur := range s[1:] {
if cur != last {
return false
}
last = cur
}
return true
}

// DO NOT delete or modify the contents of the main function!
func main() {
const maxLen = 3

slice1 := make([]string, maxLen)
fmt.Scanln(&slice1[0], &slice1[1], &slice1[2])

slice2 := make([]int, maxLen)
fmt.Scanln(&slice2[0], &slice2[1], &slice2[2])

fmt.Println(allEqual(slice1), allEqual(slice2))
}
___

Create a free account to access the full topic