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,466,932 Responses

  1. You’ll Be Unable To Guess Sash Window Refurbishment’s Tricks sash window refurbishment

  2. 제주가 삼다도란 말도 다 옛 말입니다. 남자도 많아요!! 제주도호빠와서 확인 해보실래요?

  3. Dustin表示:

    I’m really impressed with your writing skills as well as with the layout on your blog.
    Is this a paid theme or did you customize it yourself?
    Either way keep up the nice quality writing, it is rare to see a great blog like this
    one these days.

  4. JasonUnsoG表示:

    https://slotsiteleri25.com/# en cok kazand?ran slot oyunlar?

  5. Berkat rentang lebar https://hadiyasfoundation.org/ pemasok mesin skala ini, your
    choice menjadi everything dan more banyak dan tidak terbatas.

  6. slot maxwin表示:

    Pretty! This has been an extremely wonderful post. Thank
    you for providing this info.

  7. Walter Shaw表示:

    A dependable San Diego car shipping can significantly reduce the stress of relocating your vehicle

  8. Veronica表示:

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

  9. Wallacefex表示:

    en cok kazand?ran slot oyunlar?: guvenilir slot siteleri – slot oyunlar? puf noktalar?

  10. Hello i am kavin, its my first time to commenting anyplace, when i read
    this paragraph i thought i could also make comment due to this good article.

  11. Thank you for some other informative website.
    Where else may just I get that kind of information written in such an ideal manner?
    I’ve a venture that I’m simply now running on, and I’ve
    been on the glance out for such information.

  12. olymp casino表示:

    Hi there, yup this piece of writing is really fastidious and I have learned lot of
    things from it regarding blogging. thanks.

  13. Lillian Myers表示:

    This blog made me realize how important it is to have a reliable roofer like Olympia Roofing Company in your contacts! Commercial roofing Olympia

  14. FrankEmine表示:

    deneme bonusu veren siteler: yeni deneme bonusu veren siteler – yat?r?ms?z deneme bonusu veren siteler

  15. вывод. из. запоя. на. дому. ростов. http://vyvod-iz-zapoya-rostov27.ru .

  16. Wallacefex表示:

    sweet bonanza giris: sweet bonanza yorumlar – sweet bonanza guncel

  17. Hi there, just became aware of your blog through Google, and found that it is
    really informative. I am gonna watch out for brussels.

    I’ll appreciate if you continue this in future. Numerous people will be benefited from your
    writing. Cheers!

  18. Joe Butler表示:

    Amazing service! **ANYKEYWORD** really stepped up when we found ourselves in need unexpectedly portable toilet rental

  19. HiOP表示:

    VIP Experience HiOP

  20. Hello to every body, it’s my first go to see of this weblog; this weblog contains amazing and truly
    excellent data in favor of readers.

  21. Janna表示:

    You’ve made some good points there. I looked
    on the web for additional information about the issue
    and found most individuals will go along with your views on this site.

  22. Maude Hale表示:

    Just established some beautiful bamboo floors from Winnipeg Flooring Stores Curtis Carpets —so satisfied with

  23. Your insights into the heritage of asphalt are fascinating! I came throughout a cool link that explores its evolution over time asphalt company jacksonville fl

  24. Wallacefex表示:

    deneme bonusu veren yeni siteler: yat?r?ms?z deneme bonusu veren siteler – yat?r?ms?z deneme bonusu veren siteler

  25. Simply desire to say your article is as surprising. The clarity in your post is just great and i could assume you are an expert on this subject.
    Well with your permission allow me to grab
    your feed to keep up to date with forthcoming post.
    Thanks a million and please continue the gratifying work.

  26. Aiden表示:

    I was very pleased to find this web site. I need to to thank you for ones time
    for this particularly fantastic read!! I definitely appreciated every bit of it and I have you bookmarked to
    look at new stuff on your website.

  27. BradleyEdino表示:

    denemebonusuverensiteler25 denemebonusuverensiteler25 yeni deneme bonusu veren siteler

發佈留言

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