Separate the loops, like this:
<?php
$cat1 = $cat;
$args = array( 'posts_per_page' => -1, 'category' => $cat1 );?>
$myposts = get_posts( $args );
$myterms = array();
foreach ( $myposts as $mypost ) {
foreach ( wp_get_object_terms($mypost->ID, 'Brands') as $term ) {
$myterms[ $term->term_id ] = $term;
}
}
echo '<ul>';
foreach( $myterms as $term ) {
echo '<li><a href="'.get_term_link($term->slug, 'Brands').'">'.$term->name.'</a></li>';
}
echo '</ul>';
?>
If there are many terms, the approach above can result in PHP memory error. Here is another approach:
<?php
$cat1 = $cat;
$args = array( 'posts_per_page' => -1, 'category' => $cat1 );?>
$myposts = get_posts( $args );
$visited_term_ids = array();
echo '<ul>';
foreach ( $myposts as $mypost ) {
foreach ( wp_get_object_terms($mypost->ID, 'Brands') as $term ) {
if ( ! isset( $visited_term_ids[ $term->term_id ] ) ) {
echo '<li><a href="'.get_term_link($term->slug, 'Brands').'">'.$term->name.'</a></li>';
$visited_term_ids[ $term->term_id ] = TRUE;
}
}
}
echo '</ul>';
?>