How do I avoid duplicates when using a reference field? I have a content type with an entity reference field. If I have more then one entity in this field, and display the field in a view, it displays  duplicates.

Anyone figured out how to remove these duplicates in Drupal 8?
I removed the duplicates using hook_views_pre_render().

function hook_views_pre_render(\Drupal\views\ViewExecutable $view) { 
  if ($view-&gt;id() == 'my_view_id') {
    $filtered_nids = array();

    foreach ($view-&gt;result as $key =&gt; $row) {
      if (in_array($row-&gt;nid, $filtered_nids)) {
        unset($view-&gt;result[$key]); // remove the duplicate
      } else {
        $filtered_nids[] = $row-&gt;nid;
      }
    }
  } 
}


Probably not the best solution, but it works for my case.