This solution is correct. I've checked the result for the 2. test vector a in Matlab and it gives correct value.
Hello, erqnzgaa svruehal.
On the penultimate line of your code,
b(find(b==0)) = [];
will indeed remove all zeros from the output vector.
However, in Test Case 2 the input vector itself already contains zero values. Hence, there must be exactly one occurrence of zero in the output vector from the function.
In your code there is no way to distinguish the 'genuine' zero values from the 'marker' zero values that you introduce. Hence, in the penultimate line removing all zeros leads to an incorrect result.
A quick way to fix your code would be to choose as a 'marker' something that does not appear in the input vector. For example, any of: NaN, -9999, 1234. This is not really recommended, as it is not robust to give the correct answer to any general input vector (with the possible exception of using NaN as marker under the condition that NaN in the input vector should be ignored).
Another way to fix your code would be to get rid of the code that sets elements to 0 (or any other marker value), and instead shift the code which sets elements to [] (thereby directly removing those elements) so that it appears within your innermost loop.
—DIV
Used the in-build function of MATLAB, this problem can be easy done .
I am wonder that the best answer is size: 10. How did him did it !
Test | Status | Code Input and Output |
---|---|---|
1 | Pass |
%%
a = [5 3 6 4 7 7 3 5 9];
b_correct = [5 3 6 4 7 9];
assert(isequal(dedupe(a),b_correct));
|
2 | Fail |
%%
a = [1 0 0 0 1 1 1 1 0 0 1 2 2 0 1]
b_correct = [1 0 2];
assert(isequal(dedupe(a),b_correct));
a =
1 0 0 0 1 1 1 1 0 0 1 2 2 0 1
|
3 | Pass |
%%
a = [-1 -1 -1 -1 -1 -1];
b_correct = [-1];
assert(isequal(dedupe(a),b_correct));
|
277 Solvers
Increment a number, given its digits
506 Solvers
245 Solvers
Implement simple rotation cypher
806 Solvers
Sum the numbers on the main diagonal
375 Solvers