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,376,433 Responses

  1. DanielAbice表示:

    https://clomidonpharm.com/# can i buy generic clomid without rx

  2. EdgarWat表示:

    Интересные статьи о займах – https://www.bnkomi.ru/data/relize/140874/

  3. DanielAbice表示:

    https://cipharmdelivery.com/# cipro online no prescription in the usa

  4. HomeDecor表示:

    I am not sure where you’re getting your info, but good
    topic. I needs to spend some time learning more or understanding more.
    Thanks for great info I was looking for this info for my mission.

  5. GSU表示:

    Quality content is the main to invite the users to go
    to see the site, that’s what this web page is providing.

  6. Excellent weblog here! Additionally your web site loads up very fast!
    What host are you the usage of? Can I get your affiliate hyperlink
    to your host? I wish my website loaded up as fast as yours lol

    Here is my web page: alpha bites buy

  7. Pills information. Generic Name.
    where can i get cheap compazine price
    Actual information about medicines. Get information now.

  8. Caleb表示:

    What’s The Current Job Market For Inbuilt Fridge Freezer
    Professionals Like? inbuilt fridge freezer (Caleb)

  9. Nikefut表示:

    dark web search engine dark market onion dark web link

  10. Max Lawrence表示:

    “Anyone else find it challenging to choose between styles? A good ###nyKeyword### could help clarify things!” Minimalist fencing contractors

  11. Scott McBride表示:

    Just saw a before-and-after of a pressure cleaned home, and wow, the distinction is unbelievable! More about this at pressure washing conway

  12. How New Crypto Casino Altered My Life For The Better Best Crypto Casino Games

  13. Today, I went to the beach front with my children.
    I found a sea shell and gave it to my 4 year old daughter and said “You can hear the ocean if you put this to your ear.” She placed the
    shell to her ear and screamed. There was a hermit crab inside and it pinched her ear.
    She never wants to go back! LoL I know this is completely
    off topic but I had to tell someone! https://Utahsyardsale.com/author/celestev758/

  14. DanielAbice表示:

    http://amoxstar.com/# buy amoxicillin from canada

  15. You have made some really good points there.
    I checked on the internet for more info about the issue and
    found most individuals will go along with your views on this web site. https://Hariharparagovernmentiti.com/2024/12/08/clinique-esthetique-en-france-votre-destination-pour-des-traitements-esthetiques-de-qualite-2/

  16. Volvo new key表示:

    The Biggest Issue With Key Fob Volvo And How You Can Resolve It
    Volvo new key

  17. You can even make little changes to the door framework without a license.

  18. Ivan Lyons表示:

    With so many builders out there home builders in houston area

  19. Kate Cruz表示:

    After seeing all these before-and-after photos online how to clean a roof

  20. Whats up this is kinda of off topic but I was wondering if blogs use WYSIWYG
    editors or if you have to manually code with HTML.
    I’m starting a blog soon but have no coding expertise
    so I wanted to get guidance from someone with experience.
    Any help would be greatly appreciated!

  21. See What Long-Range Mobility Scooters Tricks The Celebs Are Utilizing long-range Mobility scooters

  22. Lena Taylor表示:

    Thanks for the comprehensive read. Find more at cost of pressure washing

發佈留言

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