Which values occur exactly three times?
Return a list of all values (sorted smallest to largest) that appear exactly three times in the input vector x. So if
x = [1 2 5 2 2 7 8 3 3 1 3 8 8 8]
then
y = [2 3]
x = [1 2 5 2 2 7 8 3 3 1 3 8 8 8]
then
y = [2 3]
MATLAB CODE:
clc
clear all
close all
x=input('Enter the vector');
w=sort(x);
u=[];
p=[];
y=[];
c=1;
for i=1:length(w)-1
if(w(i)==w(i+1))
c=c+1;
elseif(w(i)~=w(i+1))
u=[u w(i)];
p=[p c];
c=1;
end
end
u=[u w(length(w))];
p=[p c];
for j=1:length(p)
if(p(j)==3)
y=[y u(j)];
end
end
Explanation:
Sample Output:
Function Implementation:
function y = threeTimes(x)
w=sort(x);
u=[];
p=[];
y=[];
c=1;
for i=1:length(w)-1
if(w(i)==w(i+1))
c=c+1;
elseif(w(i)~=w(i+1))
u=[u w(i)];
p=[p c];
c=1;
end
end
u=[u w(length(w))];
p=[p c];
for j=1:length(p)
if(p(j)==3)
y=[y u(j)];
end
end
end
No comments