Remove duplicated values from meta box values

Solution

I created array outside the while loop and checked if the value is in array or not.

<?php
  $args = array('post_type' => 'posttype');
  $emptyvalue = "";
  $optionname = "optionname";
  $the_query = new WP_Query($args);

  $output ="<select name="".$optionname.""> <option                                                                  value="".$emptyvalue."">Location</option>'";

  $arr = array();
  while ( $the_query->have_posts() ) : $the_query->next_post();

  $id= $the_query->post->ID;

  $location = get_post_meta($id, 'institution_location', true);

  if(!in_array($location, $arr)){
  array_push($arr, $location);
  $get_location = $_GET['institution-location'];
  $selected = ($get_location == $location) ? 'selected=selected' : '';
  $output .="<option $selected name="".$location."" value="".$location."">".$location."   </option>";
  }

  endwhile;

   $output .="</select>";

  echo $output;

?>