Fast checking if array has repeated elements or not
조회 수: 27 (최근 30일)
이전 댓글 표시
I need to check whether a given array, A has repeated elements or not and return in true or false. The function runs near about 5*10^6 times. Initially I tried this
function flag = hasNoRepeats(A)
flag = numel(A)==numel(unique(A)) ;
end
This is very slow, it takes about 90-100 seconds. It is given that array has all positive integers and they are in between 1 and K.
function flag = hasNoRepeats(A,K)
tmp = zeros(1,K);
flag = true;
for i=1:numel(A)
if tmp(A(i))
flag = false;
break
end
tmp(A(i)) = 1;
end
end
This version is faster (~20 secs) but still slower than my requirements. Is there any way to do it faster?
참고 항목
카테고리
Help Center 및 File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!