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,364,632 Responses

  1. PF&A Design表示:

    wonderful post, very informative. I ponder why the other specialists of this
    sector do not notice this. You must proceed your writing.
    I am confident, you’ve a huge readers’ base already!

  2. I really like your blog.. very nice colors & theme. Did you make this website yourself or did you hire
    someone to do it for you? Plz reply as I’m looking to construct my own blog and would like to know where u got this from.
    thanks

  3. StevenCoact表示:

    Забор Крови на Дому – Медицинский Центр Био-Лайн
    [url=https://bio-line.org/get-blood/]Больше>>>[/url]

  4. BrandenSlomy表示:

    priligy max pharm: priligy maxpharm – buy priligy max pharm

  5. Pills information sheet. Long-Term Effects.
    where buy generic trileptal without a prescription
    All about meds. Read information here.

  6. Sazrmjw表示:

    Легальные способы покупки диплома о среднем полном образовании

    edpt204w19.mn.co/posts/69999314

  7. packman carts表示:

    I think this is among the most vital information for me.
    And i’m glad reading your article. But should remark on few general things,
    The website style is great, the articles is really excellent : D.

    Good job, cheers

  8. Dereketerm表示:

    clomid generics: clomid online – how can i get cheap clomid online

  9. I was recommended this web site by my cousin. I’m not sure
    whether this post is written by him as no one else know such detailed about my difficulty.
    You’re incredible! Thanks!

  10. StevenNub表示:

    dapoxetine online: buy dapoxetine online – priligy

  11. StevenNub表示:

    prednisone 20: Prednisone Without Prescription – prednisone 10 mg tablet cost

  12. Sazrshu表示:

    Реально ли приобрести диплом стоматолога? Основные этапы
    avtobestnews.ru/ofitsialnyiy-diplom-dlya-novoy-zhizni-dostupno-kazhdomu

  13. Violette表示:

    A motivating discussion is definitely worth comment. I
    do think that you need to write more on this subject matter, it may not be a taboo subject but typically people don’t discuss such topics.
    To the next! Best wishes!!

  14. Dereketerm表示:

    priligy max pharm: buy priligy max pharm – cheap priligy

  15. BrandenSlomy表示:

    order amoxicillin no prescription: Com Pharm – can you buy amoxicillin over the counter in canada

  16. excellent issues altogether, you simply won a brand new reader.
    What could you suggest in regards to your publish that you made a few days in the past?
    Any positive?

  17. Normally I don’t learn post on blogs, however I
    wish to say that this write-up very pressured
    me to try and do it! Your writing style has been surprised me.
    Thank you, quite great article.

  18. BrandenSlomy表示:

    cost of generic clomid tablets: generic clomid – how to get generic clomid price

  19. Sazrrkq表示:

    Процесс получения диплома стоматолога: реально ли это сделать быстро?

    publikacii.listbb.ru/viewtopic.php?f=3&t=1061

  20. BrandenSlomy表示:

    priligy: dapoxetine online – priligy maxpharm

  21. Dereketerm表示:

    buy priligy max pharm: buy priligy – max pharm

  22. Nikefut表示:

    darknet market list dark market 2024 https://darknetmarketstore.com/ – darknet links

發佈留言

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