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,238,581 Responses

  1. KevinSounc表示:

    http://denemebonusuverensiteler.win/# deneme bonusu veren siteler

  2. The 10 Scariest Things About Double Glazing Repair Near
    Me Replacing Windows With Double Glazing (https://Ktsmc.Com/Bbs/Board.Php?Bo_Table=Free&Wr_Id=282849)

  3. What’s The Job Market For Double Glazing Repairs Near Me Professionals?
    double Glazing repairs Near Me

  4. Darrelherse表示:

    en cok kazandiran slot siteleri: canl? slot siteleri – en iyi slot siteleri 2024

  5. The Reason The Biggest “Myths” Concerning Mesothelioma Legal Could Actually Be True
    mesothelioma lawsuits (http://www.Ecornd.co.kr)

  6. KevinSounc表示:

    http://sweetbonanza.network/# sweet bonanza slot demo

  7. KevinSounc表示:

    http://sweetbonanza.network/# sweet bonanza taktik

  8. You’ll Be Unable To Guess Ferrari Key Replacement Near Me’s Tricks ferrari key replacement Near me

  9. 5 Killer Quora Answers On Replacement Double Glazing Units Near Me double glazing units near me

  10. “The Ultimate Cheat Sheet On Adhd Symptoms In Adults Test signs and symptoms of adhd in adults

  11. 10 Healthy Habits For Double Glazing High Wycombe Window Fitters High Wycombe

  12. Площадки с азартными играми в виртуальном пространстве регулярно подвергает блокировке надзорный орган — РКН, действия взломщиков — тоже наблюдаются нередко.

  13. Darrelherse表示:

    canl? slot siteleri: slot oyun siteleri – guvenilir slot siteleri

  14. жби изделия цена http://www.kupit-zhbi.ru/ .

  15. Hey there just wanted to give you a quick heads up. The text in your content seem to be running off
    the screen in Chrome. I’m not sure if this is a format issue
    or something to do with browser compatibility but I figured
    I’d post to let you know. The layout look great though!
    Hope you get the issue resolved soon. Many thanks

  16. Excellent post. I was checking continuously this weblog and I am
    inspired! Very helpful info specifically the remaining section 🙂 I deal
    with such info much. I used to be seeking this certain information for a long time.

    Thank you and best of luck.

  17. Awesome issues here. I am very satisfied to look your article.
    Thank you so much and I’m looking forward to touch you.
    Will you please drop me a mail?

  18. How You Can Use A Weekly Replacement Vauxhall Key Project Can Change Your Life Vauxhall Car Keys

  19. Darrelherse表示:

    en iyi slot siteler: oyun siteleri slot – bonus veren slot siteleri

  20. That is really fascinating, You are a very skilled blogger.
    I’ve joined your feed and look ahead to searching for more of your fantastic post.

    Also, I have shared your website in my social networks

  21. KevinSounc表示:

    http://slotsiteleri.bid/# slot casino siteleri

  22. Anxiety Depression Symptoms: The Good, The Bad, And The Ugly depressive Disorder Symptoms

  23. Hi everybody, here every one is sharing these kinds of knowledge, so it’s
    pleasant to read this webpage, and I used to go to see this website daily.

  24. Do you have any video of that? I’d love to find out some additional information.

  25. Hey there! Someone in my Myspace group shared this website with us so I came to take a
    look. I’m definitely enjoying the information. I’m book-marking and will be tweeting this to
    my followers! Exceptional blog and brilliant design and style.

  26. I don’t know if it’s just me or if perhaps everybody else encountering problems with your blog.
    It appears like some of the written text on your
    posts are running off the screen. Can someone else please provide feedback and
    let me know if this is happening to them as well? This may be a problem with my
    web browser because I’ve had this happen before.
    Cheers

  27. yhcns.Kr表示:

    20 Fun Infographics About ADHD Symptoms In Adult Women adhd in women assessment, yhcns.Kr,

發佈留言

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