shineYao

unique(未完成)

const unique = arr => {
    const sortedArr = arr.sort((a, b) => a > b);
    const first = sortedArr[0];
    let last = sortedArr[sortedArr.length - 1];
    const result = [];
    result.push(last);
    for(let len = sortedArr.length, i = len - 2; i > 0; i--) {
        const temp = sortedArr[i];
        if(last === temp) {
            continue;
        } else {
            if(last > temp) {
                result.push(temp);
                if(first === temp) {
                    break;
                } else {
                    last = temp;
                }
            }
        }
    }
    return result;
}

posted on 2017-09-16 21:18  shineYao  阅读(111)  评论(0编辑  收藏  举报

导航