Problem 838. Check if number exists in vector
Return 1 if number a exists in vector b otherwise return 0.
a = 3;
b = [1,2,4];
Returns 0.
a = 3;
b = [1,2,3];
Returns 1.
Solution Stats
Problem Comments
-
9 Comments
Show
6 older comments
Richard Zapor
on 17 Jul 2012
Add test vector a = -12;
b = [1,3,4,5,6,7,8,-12,2]; and rescore.
Better is add a=-randi(16); b= [1 2 3 a];
These will eliminate answers like #6.
frank mcnally
on 15 Aug 2019
Tests allow incorrect solution to pass:
function y = existsInVector(a,b)
y=0
for i = 1:numel(b);
if i==a
y=1
break
end
end
end
Jyothsna Chintala
on 21 Apr 2020
good
Nicholas Bitler
on 26 Nov 2020
that was fun, took me a couple minutes
Qingyin Zhang
on 29 Nov 2020
y = sum(b == a);
Colin McNicholas
on 13 Dec 2021
Not too bad
Hung Van Le
on 24 Feb 2022
yay
Aman Yadav
on 31 Mar 2024
fggga
MOHAMMAD SAMEER
on 22 May 2024
As difference in solution directly proportional to size of solution
Solution Comments
Show commentsProblem Recent Solvers11539
Suggested Problems
-
All your base are belong to us
535 Solvers
-
509 Solvers
-
5103 Solvers
-
1758 Solvers
-
Find x rows where the sum of the numbers is the maximum
422 Solvers
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!