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,612 Responses

  1. I got this site from my friend who told me on the topic of this website and now this time I am browsing this site and
    reading very informative articles or reviews here.

  2. The United State Fda (FDA) has not authorized any of these
    items.

  3. Hmm it seems like your website ate my first comment (it was extremely long)
    so I guess I’ll just sum it up what I had written and say, I’m thoroughly
    enjoying your blog. I too am an aspiring blog blogger but I’m still new to everything.

    Do you have any recommendations for newbie blog writers?
    I’d definitely appreciate it.

  4. Jay Sherman表示:

    If you’ve been involved in a car accident in Seattle, it’s crucial to seek help from a qualified chiropractor. They can provide the necessary care to address any pain or discomfort resulting from the incident Seattle Car Accident Chiropractor

  5. Steven Malone表示:

    This was beautifully organized. Discover more at ift physiotherapy

  6. Cole McDaniel表示:

    I recently completed a kitchen remodel on Mercer Island, and the transformation is unbelievable! It’s amazing how a new layout and updated appliances can completely change the feel of a home Remodel Contractor

  7. Jane McBride表示:

    Well done! Find more at roof repair

  8. JasonUnsoG表示:

    http://slotsiteleri25.com/# slot oyunlar? puf noktalar?

  9. 10 Back Injury Lawsuits Tricks All Experts Recommend Injury Lawyers
    (Squareblogs.net)

  10. Altid har et overblik over forbrug og fakturaer uden at gРµ pРµ kompromis. Uden Recept Online brGenerisk Alternativ Til Topamac Brug A Amexbr. Levitra cialis apotek Levitra bald billiger Para que sirve el levitra Cada, custa. Inderal Utan Recept, Apotek Online Sverige, liKolo Carts. Usa. Prices the sildenafil tablets buy bulk, Potenspiller uden recept, Nord Apotek. https://danmarkgenerisk.com/ancozan/ Kobe Ancozan Pille

  11. JasonUnsoG表示:

    https://casinositeleri25.com/# deneme bonusu veren casino siteleri

  12. JasonUnsoG表示:

    http://casinositeleri25.com/# Casino Siteleri

  13. This design is steller! You obviously know how to keep a reader
    amused. Between your wit and your videos, I
    was almost moved to start my own blog (well, almost…HaHa!) Wonderful job.
    I really enjoyed what you had to say, and more
    than that, how you presented it. Too cool!

    My page – temporary floor protection

  14. Jesse Hudson表示:

    Loved your pointers on stopping split ends; they’re awesome life like and trouble-free to put into effect! More care ideas may be came upon at hair salon in west vancouver

  15. kurumsal web tasarım beylikdüzü

    I am no longer positive where you’re getting your info, but great topic.
    I needs to spend some time learning more or understanding
    more. Thank you for excellent information I used to be in search of
    this information for my mission.

  16. JasonUnsoG表示:

    http://sweetbonanza25.com/# sweet bonanza oyna

  17. BradleyEdino表示:

    deneme bonusu veren siteler deneme bonusu veren yeni siteler deneme bonusu veren siteler yeni

  18. Blanche Lewis表示:

    The concept of multi-level decks was fascinating; it adds so much depth deck builder in charlotte

  19. google sertifikalı kurumsal web tasarım

    I all the time used to study paragraph in news papers
    but now as I am a user of internet so from now I am using net for
    posts, thanks to web.

  20. UG8表示:

    If some one needs expert view on the topic of running a blog then i
    advise him/her to pay a visit this web site, Keep up the nice work.

  21. BradleyEdino表示:

    en iyi bahis uygulamasД± Casino Siteleri Deneme Bonusu Veren Siteler

  22. Luis Keller表示:

    Nunca pensei que a alimentação influenciasse tanto na saúde bucal. Vou ficar mais atento agora e procurar uma Facetas de Resina maristas para orientações

  23. How To Outsmart Your Boss On Train Injury Lawsuit 18 Wheeler Truck Accident Attorney

發佈留言

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