/**
 * Given a list of different habitats, this function returns the count of species
 * that are common to all the habitats.
 *
 * @param habitats A list of lists, where each inner list contains species found in a habitat.
 * @return Number of species that are common to all habitats.
 */
fun countCommonSpecies(habitats: List<List<String>>): Int {

    if (habitats.size <= 1) {
        return 0
    }

    val commonSpecies = findCommonSpecies(habitats[0], habitats[1])
    for (i in 2 until habitats.size) {
        commonSpecies.retainAll(habitats[i])
    }

    return commonSpecies.size
}

fun findCommonSpecies(habitat1: List<String>, habitat2: List<String>): MutableSet<String> {
    return habitat1.intersect(habitat2).toMutableSet()
}
/**
 * Given a list of different habitats, this function returns the count of species
 * that are common to all the habitats.
 *
 * @param habitats A list of lists, where each inner list contains species found in a habitat.
 * @return Number of species that are common to all habitats.
 */
fun countCommonSpecies(habitats: List<List<String>>): Int {
/**
 * Given a list of different habitats, this function returns the count of species
 * that are common to all the habitats.
 *
 * @param habitats A list of lists, where each inner list contains species found in a habitat.
 * @return Number of species that are common to all habitats.
 */
fun countCommonSpecies(habitats: List<List<String>>): Int {

    if (habitats.size <= 1) {
        return 0
    }

    val commonSpecies = findCommonSpecies(habitats[0], habitats[1])
    for (i in 2 until habitats.size) {
        commonSpecies.retainAll(habitats[i])
    }

    return commonSpecies.size
}

fun findCommonSpecies(habitat1: List<String>, habitat2: List<String>): MutableSet<String> {
    return habitat1.intersect(habitat2).toMutableSet()
}