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,245,714 Responses

  1. StevenNub表示:

    prednisone 2.5 mg cost: order Prednisone – where to buy prednisone uk

  2. Dereketerm表示:

    prednisone 12 mg: ray pharm – generic over the counter prednisone

  3. BrandenSlomy表示:

    buy amoxicillin without prescription: Amoxicillin Com Pharm – amoxicillin 500mg capsule

  4. Nikefut表示:

    tor markets 2024 best darknet markets https://darknetmarketstore.com/ – dark website

  5. StevenNub表示:

    where can i get cheap clomid without dr prescription: clomid purchase online rex pharm – where can i get generic clomid without insurance

  6. StevenNub表示:

    prednisone 5 50mg tablet price: prednisone – order prednisone online canada

  7. BrandenSlomy表示:

    prednisone 50 mg tablet canada: prednisone ray pharm – prednisone 30 mg daily

  8. Magnificent items from you, man. I’ve bear in mind your stuff
    previous to and you are simply too fantastic. I actually like what you’ve acquired here, really like what you are stating and the best way
    wherein you say it. You’re making it enjoyable and you continue to take
    care of to stay it sensible. I cant wait to read much more from you.
    That is really a tremendous website.

  9. eb5 visa cost表示:

    I know this web site provides quality depending articles and other material, is there any other web page which provides
    these data in quality?

  10. Hi there, yes this article is actually fastidious and I have learned lot of things from it concerning blogging.
    thanks.

  11. Hi, of course this piece of writing is really good and I have learned lot of things from it
    on the topic of blogging. thanks.

  12. BrandenSlomy表示:

    buy priligy: max pharm – buy priligy

  13. Very rapidly this website will be famous among all blogging and site-building users,
    due to it’s fastidious posts

  14. wps表示:

    You can definitely see your expertise in the article you write.

    The arena hopes for even more passionate writers such as you who aren’t afraid to say how they believe.
    All the time follow your heart.

  15. StevenNub表示:

    compare prednisone prices: raypharm – prednisone 10mg tabs

  16. BrandenSlomy表示:

    max pharm: priligy maxpharm – dapoxetine online

  17. LeonardKnism表示:

    tsmk-altai.ru/ – Закажите кухню, которая станет настоящей гордостью вашего дома.

  18. Thank you for another informative web site.
    The place else may just I get that type of information written in such an ideal way?

    I have a undertaking that I’m simply now running on,
    and I have been at the glance out for such info.

  19. Dereketerm表示:

    where to buy generic clomid: cheap clomid – can i buy cheap clomid without dr prescription

  20. BrandenSlomy表示:

    order cheap clomid now: clomid purchase online rex pharm – can you buy generic clomid online

  21. StevenNub表示:

    prednisone 20 mg purchase: prednisone – generic prednisone 10mg

  22. StevenNub表示:

    non prescription prednisone 20mg: order Prednisone – prednisone pills 10 mg

發佈留言

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