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,247,735 Responses

  1. Michael Memos表示:

    damansara heights house for sale

  2. AlbertDuM表示:

    ivermectin human: IverFast – buy ivermectin uk

  3. Hi, i believe that i noticed you visited my blog so i got here to go
    back the prefer?.I’m trying to in finding issues to improve my site!I
    suppose its adequate to use some of your ideas!!

  4. Howdy! I know this is kinda off topic nevertheless I’d figured
    I’d ask. Would you be interested in exchanging links or maybe guest writing a blog post or vice-versa?
    My site goes over a lot of the same subjects as yours and I
    believe we could greatly benefit from each other. If you
    are interested feel free to send me an e-mail. I look
    forward to hearing from you! Terrific blog
    by the way!

  5. Sazrdiw表示:

    Пошаговая инструкция по официальной покупке диплома о высшем образовании

    forexsnews.ru/sdelayte-shag-vpered-poluchite-diplom-byistro

  6. I have read so many content regarding the blogger lovers however this article is actually a pleasant piece of
    writing, keep it up.

  7. I’d like to find out more? I’d like to find out more details.

  8. BernardMop表示:

    Abortion pills online buy misoprostol over the counter order cytotec online

  9. BernardMop表示:

    generic plavix PlavixClo Cost of Plavix on Medicare

  10. JacobLoody表示:

    http://www.taxipuma.ru — качество и стиль от производителя

  11. Johanna Byrd表示:

    Appreciating all these referrals customized in the direction of particular areas– really thoughtful technique!” ### anyKeyWord Hill Country Flooring & Construction

  12. Harry Reese表示:

    Great article! I’ve been looking for a reliable roofing company and your tips are really helpful licensed roofing contractors near me

  13. AlbertDuM表示:

    buy Lisinopril online: lisinopril1st – Lisinopril 1st

  14. Drug information for patients. Short-Term Effects.
    cost of zithromax without dr prescription
    Actual news about meds. Read information here.

  15. DarrenUnjuh表示:

    https://lisinopril1st.com/# lisinopril 4 mg

  16. BrandenSlomy表示:

    dapoxetine price: cheap priligy – buy priligy

  17. This is very attention-grabbing, You are a very professional blogger.

    I have joined your feed and stay up for in quest of extra of your wonderful post.

    Also, I have shared your web site in my social networks

  18. DarrenUnjuh表示:

    https://cytpremium.com/# cytotec pills buy online

發佈留言

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