Get number of root elements in walker class

I’m not sure if this is what you’re after. You can use $this to reference the current walker instance.

class YPE_custom_navwalker extends Walker_Nav_Menu {
    public function start_lvl(&$output, $depth=0, $args=array()) {

        $items = wp_get_nav_menu_items( $args->menu->term_id );
        echo $this->get_number_of_root_elements( $items );

        parent::start_lvl(&$output, $depth,$args);
    }
}