Get list of all Topics in use by a custom post type

I think I cracked it actually! Used this snippet:

<?php 
  $custom_terms = get_terms('topic');

foreach($custom_terms as $custom_term) {
wp_reset_query();
$args = array('post_type' => 'episodes',
    'tax_query' => array(
        array(
            'taxonomy' => 'topic',
            'field' => 'slug',
            'terms' => $custom_term->slug,
        ),
    ),
 );

 $loop = new WP_Query($args);
 if($loop->have_posts()) {
    echo '<h2>'.$custom_term->name.'</h2>';


 }
}
?>