MATLAB: Get rid of an unwanted zero element in an IF loop

matlab r2013b

I'm trying to create a function that returns only the elements in a vector which repeats exactly three times using the following code:
function y = three_times(x)
a = sort(x)
b = unique(a)
for i = 1:numel(b)
c = find(b(i) == a)
d = length(c)
if d == 3
e(i) = b(i)
end
end
y = e
If I apply this function to a vector like
x = [1 2 5 2 2 7 8 3 3 1 3 8 8 8]
I get y = [0 2 3] instead of y = [2 3]. Any hints on how can I repair this?

Best Answer

x = [1 2 5 2 2 7 8 3 3 1 3 8 8 8]
[freq,a]=hist(x,unique(x))
out=a(freq==3)