JS 依照元素出現數量排序

同樣是最近遇到的狀況,需要利用陣列內元素出現的數量進行排序,以下為範例程式:

var array = ["1", "2", "4", "5", "2", "4", "2", "3"];
var str = array.toString();
var filter = [];
var result = [];
document.write("原始陣列:" + array + "<br>");

var get = function (str, tar, arr, tmp) {
    if (str.indexOf(tar) >= 0) {
        tmp = str.slice(str.indexOf(tar) + 1);
        arr.push(tar);
        get(tmp, tar, arr, tmp);
    }
    return arr;
}

for (i in array) {
    var elm = [];
    var tmp;
    var fstr = filter.join();
    if (fstr.indexOf(array[i]) >= 0) {
        continue;
    } else {
        var tmp_arr = get(str, array[i], elm, tmp);
        result.push(tmp_arr.length + ':' + tmp_arr[0]);
        filter.push(array[i]);
    }
}

result.sort();
result.reverse();

//按照出現次數排序,不刪除重複元素
var finishArray = [];

for (index in result) {
    var show = result[index].split(':');
    for (i = 0; i < show[0]; i++) {
        finishArray.push(show[1]);
    }
}
document.write("按出現次數排序,不刪除重複元素:" + finishArray + "<br>");

//按照出現次數排序,刪除重複元素
var finishArray = [];
for (index in result) {
    var show = result[index].split(':');
    finishArray.push(show[1]);
}
document.write("按出現次數排序,刪除重複元素:" + finishArray + "<br>");

輸出結果:

原始陣列:1,2,4,5,2,4,2,3
按出現次數排序,不刪除重複元素:2,2,2,4,4,5,3,1
按出現次數排序,刪除重複元素:2,4,5,3,1

You may also like...

1,467,126 Responses

  1. 99dewa表示:

    Very quickly this web site will be famous among all blogging and site-building people, due to it’s pleasant articles or reviews

  2. FrankEmine表示:

    az parayla cok kazandiran slot oyunlar?: az parayla cok kazandiran slot oyunlar? – az parayla cok kazandiran slot oyunlar?

  3. jav for me表示:

    What’s up everyone, it’s my first pay a visit at this website, and post is in fact fruitful
    in favor of me, keep up posting such content.

  4. I do agree with all of the concepts you’ve offered to your post.

    They’re really convincing and can definitely work. Nonetheless, the posts are too short for starters.
    Could you please lengthen them a bit from next time?
    Thanks for the post.

  5. HiOP表示:

    5 For You To Host A Yard Bbq On The Budget HiOP

  6. Jordan表示:

    Very energetic article, I enjoyed that bit. Will
    there be a part 2?

  7. It’s a pity you don’t have a donate button! I’d definitely donate to
    this superb blog! I suppose for now i’ll settle for book-marking and adding your RSS feed
    to my Google account. I look forward to new updates and will
    share this website with my Facebook group. Talk soon!

  8. вывести из запоя [url=https://vyvod-iz-zapoya-rostov229.ru]вывести из запоя[/url] .

  9. Thanks for the clear advice. More at Gordon Law

  10. 100 먹튀검증 먹튀사이트 목록 확인 커뮤니티 먹튀모음 토토사이트 메이저사이트 스포츠 미니게임 파워볼 꽁머니사이트
    모든 사이트 먹튀검증을 시행하고 있으며, 가장 빠른 먹튀검증사이트입니다 먹튀모음은 먹튀에서 안전한 공식 보증업체를 추천해드리며, 부당한 일을
    당하지 않게 체계적인 시스템으로 운영하고 있습니다

  11. Jim Hampton表示:

    Has anyone considered sustainable options when hiring a Remodeling companies

  12. Jacob Fisher表示:

    Thank you for discussing the cultural importance of marijuana in distinctive societies! It’s a prosperous topic to explore extra; see greater at weed delivery

  13. Wallacefex表示:

    en guvenilir casino siteleri: Casino Siteleri – canl? casino siteleri

  14. Michaelcib表示:

    Portugal has long been a country known for producing exceptional talent in the world of sports. With a rich history in football, many athletes from this nation have achieved international fame through their incredible skills and accomplishments http://www.asystechnik.com/index.php?title=_How_Lusitano_Horses_Excel_in_Bullfighting

  15. hiop表示:

    Bar Hopping hiop

  16. Hello, i feel that i noticed you visited my web site thus i got here to go back the
    favor?.I am attempting to to find things to enhance my website!I guess its adequate to use
    a few of your ideas!!

  17. I really like what you guys tend to be up too. This type of clever
    work and exposure! Keep up the superb works guys I’ve you guys to my
    own blogroll.

  18. нарколог на дому капельница цена нарколог на дому капельница цена .

  19. Roger Austin表示:

    Great job explaining the role of additives in concrete mixtures! Such an informative read! More details can be found at my site: concrete contractor

  20. HiOP表示:

    Everything It Is Advisable To Know About
    Melbourne Nightlife HiOP

  21. Asbestos Attorney Explained In Fewer Than 140 Characters asbestos lawsuits

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。