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,741 Responses

  1. menwiki.men表示:

    %%

    my web site – upvc Door repairs redbridge (menwiki.men)

  2. If you wish for to increase your know-how simply keep visiting this site and be updated with the newest gossip posted here.

  3. %%

    Here is my blog … asbestos legal

  4. %%

    Feel free to surf to my website; Headphone shop

  5. sga77表示:

    I really like your blog.. very nice colors & theme.
    Did you create this website yourself or did you hire
    someone to do it for you? Plz answer back as I’m looking to construct my own blog and would like to find out where
    u got this from. thanks a lot

  6. belitung表示:

    A fascinating discussion is worth comment. I do believe that you need to write more on this topic, it might not be a taboo subject
    but usually folks don’t talk about such issues.
    To the next! Many thanks!!

  7. hairdresser表示:

    Hello, I believe your blog might be having web browser compatibility
    problems. When I take a look at your website in Safari, it looks fine but when opening in I.E., it has some overlapping issues.
    I simply wanted to give you a quick heads up! Aside from
    that, great website!

  8. Buying a verified Cash App account can provide peace of mind and security for your financial
    transactions. Verified accounts offer added benefits such as higher transaction limits and the
    ability to withdraw funds instantly. With a verified Cash App account, you can easily and securely send and receive money, making it a valuable asset for anyone seeking a reliable and efficient digital payment solution.

  9. %%

    Also visit my web blog: asbestos compensation (https://www.ligra.cloud/)

  10. %%

    Feel free to surf to my website hounslow door and window
    (https://grant-haney.hubstack.net)

  11. Do you want to make hassle-free transactions with Cash
    App? Buy a verified Cash App account today! With a verified account,
    you can unlock increased transaction limits and gain additional security measures.
    Experience seamless money transfers and enjoy the convenience
    of a verified Cash App account. Don’t miss out on this opportunity, get yours now!

  12. I couldn’t resist commenting. Very well written!

  13. Nilda表示:

    in wall electric fireplace (Nilda)

  14. I am genuinely thankful to the holder of this web site who has shared this impressive post at at this time.

  15. Clemmie表示:

    ghost immobiliser installation; Clemmie,

  16. Doris表示:

    asbestosis settlement amounts (Doris)

  17. Your method of explaining everything in this piece of writing is actually good, every one be
    able to simply understand it, Thanks a lot.

  18. What is表示:

    %%

    Here is my webpage What is

  19. Heya i am for the first time here. I came across this board and I find It really helpful & it helped me out a lot.
    I’m hoping to give one thing again and aid others like you helped me.

  20. Rosemary表示:

    ghost immobiliser installation (Rosemary)

  21. Bettina表示:

    %%

    Review my homepage :: full spectrum cbd oil for
    sale uk (Bettina)

發佈留言

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