Printing non-unique elements in an array

I’m trying to print non-unique elements in an array. Consider the program below:

module nonunique;
int non_unique_array [1:10];
int i,j,count;

     initial begin 
               
           non_unique_array = '{5,2,5,2,2,6,8,9,5,9};
           $display("\n------------printing non-unique elements----------\n");
           $display ("the values inside the array are");
           for(i=1;i<=10;i++)
           begin 
              $display("%d",non_unique_array[i]);
           end
           $display("the non unique values inside the array are\n");
           for(i=1;i<=9;i++) 
            begin
              for(j=i+1;j<=10;j++)
                begin
                  count = 0;
                  if (non_unique_array[i] == non_unique_array[j])
                      begin
                         count++; 
                         if (count == 1)
                         $display("%0d", non_unique_array[i]);
                      end
                 end
               end

     end

endmodule

The output I got:

# ------------printing non-unique elements----------
# 
# the values inside the array are
#           5
#           2
#           5
#           2
#           2
#           6
#           8
#           9
#           5
#           9
# the non unique values inside the array are
# 
# 5
# 5
# 2
# 2
# 5
# 2
# 9

The output I want is:

# ------------printing non-unique elements----------
# 
# the values inside the array are
#           5
#           2
#           5
#           2
#           2
#           6
#           8
#           9
#           5
#           9
# the non unique values inside the array are
# 
# 5 
# 2
# 9

What should I do?

Thanks in advance!!

In reply to Shashank Gurijala:

You have only one counter and reseting it back to 0 each time you start the loop. You need a counter for each value in the array. Use an associative array with a counter for each value.

module nonunique; 
  int non_unique_array [1:10];
  int counter[int] = '{default:0};
 
  initial begin 
    non_unique_array = '{5,2,5,2,2,6,8,9,5,9};
    $display("\n------------printing non-unique elements----------\n");
    $display ("the values inside the array are");
    foreach (non_unique_array[i]) begin 
      $display("%d",non_unique_array[i]);
      counter[non_unique_array[i]]++;
    end
    
    $display("the non unique values inside the array are\n");
    foreach (counter[val]) begin
      if (counter[val]>1)
        $display("%0d", val);
    end
  end
 
endmodule