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,524,725 Responses

  1. Wow that was strange. I just wrote an very long comment but after I clicked submit my comment didn’t show up.
    Grrrr… well I’m not writing all that over again. Anyway, just wanted to say superb blog!

  2. секс у гинеколога [url=http://ginekolog-rukoeb1.ru]http://ginekolog-rukoeb1.ru[/url] .

  3. Dannyfut表示:

    darknet drug store https://github.com/darknetmarketslinks/darknetmarketlinks – darknet market links dark market 2025

  4. Howdy just wanted to give you a quick heads up.
    The words in your post seem to be running off the screen in Ie.
    I’m not sure if this is a formatting issue or something to do with browser compatibility but I thought I’d
    post to let you know. The design and style look great though!
    Hope you get the issue solved soon. Many thanks

  5. Davidmoits表示:

    pharmacy discount coupons: Cheapest online pharmacy – Online pharmacy USA

  6. MichaelIncap表示:

    dark web sites https://github.com/darknetmarketslinks/darknetmarketlinks – deep web search deep web drug store

  7. What’s up everybody, here every one is sharing these kinds of familiarity, thus it’s pleasant to read this webpage, and I
    used to visit this weblog daily.

  8. 10 Meetups Around Buy Category A Driving License You Should Attend comprar A carta De Condução do imt – lovewiki.faith

  9. секс у гинеколога ginekolog-rukoeb1.ru .

  10. Michaelkig表示:

    dark web search engine https://github.com/darknetmarkets2025/darknetmarketlinks – dark markets 2025 dark web market links

  11. PENIPU表示:

    An outstanding share! I have just forwarded this onto a friend who was doing a little homework on this.

    And he in fact ordered me dinner because I stumbled upon it for him…

    lol. So let me reword this…. Thank YOU for the meal!!

    But yeah, thanks for spending the time to discuss this
    topic here on your web page.

  12. Porterfax表示:

    purple pharmacy mexico price list xxl mexican pharm mexico pharmacies prescription drugs

  13. Davidmoits表示:

    discount drug pharmacy: drug mart – canadian pharmacy world coupon code

  14. Hi to every one, because I am really eager of reading this
    blog’s post to be updated regularly. It contains
    fastidious stuff.

  15. MichaelIncap表示:

    how to get on dark web https://github.com/darknetmarketslinks/darknetmarketlinks – dark market link darknet site

  16. It’s an awesome paragraph for all the online visitors; they will take advantage from it I am sure.

  17. Charleskepsy表示:

    canadian online pharmacy no prescription https://discountdrugmart.pro/# discount drugs

  18. Acheter cialis pas cher, prix tadalafil, Africaphotography Magasin. Ayez besoin dun produit ce, t disn dans le site? Appelez les USA, Pharmacie. Achat marque vente professionnel pharmacie ligne, commandez le sildenafil prix. escompte Inde, ordre 100mg pfizer achat sans ordonnance cialis sildenafil. Milical, Vente en ligne produit parapharmacie Milical. Pille Cialis pharmacie en ligne en france 100mg tabletten und Viagra kostnad. biomlicaux avec un p61e d excellence en pharmacie -pharmacologie, a. https://eurogeneriqueetfrance.com/bactrim/ Commande Bactrim Alternative

  19. Michaelkig表示:

    dark web market list https://github.com/darknetmarkets2025/darknetmarketlinks – tor markets 2025 tor markets links

發佈留言

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