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,646,765 Responses

  1. BrandenSlomy表示:

    can i get generic clomid pills: clomid purchase online rex pharm – can you buy cheap clomid without prescription

  2. Bradleygoath表示:

    amoxicillin 500mg price canada Com Pharm amoxicillin 500mg cost

  3. Узнай все о признаки варикоцеле признаки варикоцеле

  4. BrandenSlomy表示:

    875 mg amoxicillin cost: com pharm – amoxicillin 500

  5. Howdy! I know this is kind of off topic but I was wondering which blog platform are you
    using for this website? I’m getting tired of WordPress
    because I’ve had problems with hackers and I’m looking at options for
    another platform. I would be great if you could point me in the
    direction of a good platform.

  6. Markdob表示:

    Всем привет! Может кто знает, где найтиразные статьи о недвижимости? Сейчас читаю – https://azovcity-kuban.ru

  7. Markdob表示:

    Всем привет! Подскажите, где почитатьполезные блоги о недвижимости? Сейчас читаю – https://azovcity-kuban.ru

  8. I don’t even know the way I finished up here, but I thought this post was great.

    I don’t recognize who you might be but certainly you’re going to a famous blogger if you
    are not already. Cheers!

  9. fantastic putting as an entire, you just received a logo new reader. What would you recommend about your that you just made a few days ago? certain?

    My website – http://support-groups.org/memberlist.php?mode=viewprofile&u=40233

  10. Markdob表示:

    Всем привет! Подскажите, где почитатьполезные статьи о недвижимости? Сейчас читаю – https://azovcity-kuban.ru

  11. As interpretacoes dos sonhos mais populares dos livros mais populares sobre o sono
    Sonho de um gato adormecido

    [url=http://thinktoy.net/bbs/board.php?bo_table=customer2&wr_id=852061]Sonha com um cao qu[/url] [url=https://www.bodrumhavalimani.com/all-aboard-the-rocky-mountaineer/#comment-436733]Porque e que sonho em apanhar damascos?[/url] [url=https://forum-tver.ru/threads/zhenskaja-komanda-iz-tveri-zavoevala-zoloto-mezhdunarodnogo-turnira-po-peretjagivaniju-kanata.33538/page-17#post-1043854]Sonho de um gato adormecido[/url] [url=https://www.tarocchigratis.info/tennis-video-il-punto-piu-bello-di-sempre/#comment-324]Porque e que sonho em apanhar damascos?[/url] [url=https://nasvyazi.space/showthread.php?tid=71769&pid=454821#pid454821]Porque e que sonho com o kvass?[/url] 3e9f24d

  12. Markdob表示:

    Приветствую. Может кто знает, где найтиполезные статьи о недвижимости? Пока нашел – https://azovcity-kuban.ru

  13. Casino表示:

    I am really impressed with your writing skills and also with the layout on your weblog.
    Is this a paid theme or did you customize it yourself?

    Anyway keep up the nice quality writing, it’s rare to see a nice blog like this one these days.

  14. BrandenSlomy表示:

    buy prednisone without prescription paypal: generic Prednisone – prednisone 20 mg

  15. StevenNub表示:

    Priligy tablets: buy priligy max pharm – dapoxetine price

  16. Узнай все о чем опасно варикоцеле варикоцеле яичка

  17. Excellent write-up. I absolutely appreciate this website.
    Keep writing!

  18. Bradleygoath表示:

    amoxicillin in india Com Pharm how to get amoxicillin over the counter

  19. Bradleygoath表示:

    no prescription online prednisone Prednisone Without Prescription prednisone 30 mg daily

  20. BrandenSlomy表示:

    amoxicillin azithromycin: com pharm – where to buy amoxicillin over the counter

  21. TAMGARH2表示:

    Saved as a favorite, I really like your website!

  22. Medication information sheet. What side effects can this medication cause?
    cost mesalamine pills
    Best about medicament. Get information here.

  23. StevenNub表示:

    can i purchase generic clomid online: generic clomid – get generic clomid tablets

  24. Markdob表示:

    Приветствую. Подскажите, где почитатьполезные статьи о недвижимости? Пока нашел – https://azovcity-kuban.ru

  25. Markdob表示:

    Приветствую. Может кто знает, где почитатьразные блоги о недвижимости? Пока нашел – https://azovcity-kuban.ru

  26. StevenNub表示:

    where to buy clomid without insurance: where to buy clomid without dr prescription – generic clomid price

發佈留言

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