2016년 3월 29일 화요일

[스크랩]자바스크립트 중복 제거 소스코드

How do I check if an array has duplicate values?
If more than 1 element of the same exist, then return true. Otherwise, return false.
['hello','goodbye','hey'] //return false because no duplicates exist
['hello','goodbye','hello'] // return true because duplicaets exist
shareedit

marked as duplicate by Brian Roachmu is too shortJoseph SilberSasha ChedygovBen AlpertSep 11 '11 at 6:39

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
   
   
I don't want a list of duplicates removed. I just want to know true or false if a list has duplicates in it. – user847495 Sep 11 '11 at 6:08 
   
The accepted answer for the exact same question you asked is your answer.stackoverflow.com/questions/840781/… – Brian Roach Sep 11 '11 at 6:28 
   
1 
This question is not a duplicate. Since @user847495 simply wants to check if duplicates exists, the solution is faster/easier than what's needed to find all occurrences of duplicates. For example, you can do this:codr.io/v/bvzxhqm – alden Sep 26 '15 at 16:32 

3 Answers

up vote26down voteaccepted
If you have an ES2015 environment (as of this writing: io.js, IE11, Chrome, Firefox, WebKit nightly), then the following will work, and will be fast (viz. O(n)):
function hasDuplicates(array) {
    return (new Set(array)).size !== array.length;
}

If you only need string values in the array, the following will work:
function hasDuplicates(array) {
    var valuesSoFar = Object.create(null);
    for (var i = 0; i < array.length; ++i) {
        var value = array[i];
        if (value in valuesSoFar) {
            return true;
        }
        valuesSoFar[value] = true;
    }
    return false;
}
We use a "hash table" valuesSoFar whose keys are the values we've seen in the array so far. We do a lookup using Object.prototype.hasOwnProperty.call to see if that value has been spotted already; if so, we bail out of the loop and return true. (We don't use valuesSoFar.hasOwnProperty directly because that would break if the array contained "hasOwnProperty" as a string.)

If you need a function that works for more than just string values, the following will work, but isn't as performant; it's O(n2) instead of O(n).
function hasDuplicates(array) {
    var valuesSoFar = [];
    for (var i = 0; i < array.length; ++i) {
        var value = array[i];
        if (valuesSoFar.indexOf(value) !== -1) {
            return true;
        }
        valuesSoFar.push(value);
    }
    return false;
}
The difference is simply that we use an array instead of a hash table for valuesSoFar, since JavaScript "hash tables" (i.e. objects) only have string keys. This means we lose the O(1) lookup time of hasOwnProperty, instead getting an O(n) lookup time of indexOf.
shareedit

댓글 없음:

댓글 쓰기

크롬 에서 번역 옵션 뜨는 거 막는 방법

버그 같은게 아니고 저 옵션의 기본 값이 제공으로 바뀐듯... 번역 옵션 제공을 비활성화하면 안 뜸. Chrome에서 웹페이지 번역 모르는 언어로 작성된 페이지를 방문할 때 다음 단계에 따라 Chrome이 페이지를 번역하도록 할 수 있습...