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,228,860 Responses

  1. Тут можно преобрести огнеупорный сейф огнестойкий сейф купить

  2. Diplomi_taMl表示:

    где можно купить диплом воспитателя many-diplom77.ru .

  3. The 10 Most Scariest Things About 3 Seater Chaise 3 seater Chaise

  4. 5 Killer Quora Answers To Outdoor Electric Heater For
    Patio outdoor electric heater for patio

  5. rust cheats表示:

    I just could not leave your site before suggesting
    that I really loved the standard information an individual
    provide in your visitors? Is gonna be back frequently in order
    to inspect new posts

  6. Appreciating the commitment you put into your site and in depth
    information you provide. It’s awesome to come
    across a blog every once in a while that isn’t the same outdated
    rehashed material. Wonderful read! I’ve bookmarked your
    site and I’m including your RSS feeds to my Google account.

    https://w3.livetogelsgp.icu/

  7. ClydePab表示:

    ultrabet tr online: ultrabet tr online – ultrabet bonus

  8. Jerome表示:

    Why You Should Concentrate On Enhancing Triple Single Bunk
    Bed triple single bunk beds (Jerome)

  9. ClydePab表示:

    matadorbet giris: matadorbet bid – matadorbet giris

  10. Five Killer Quora Answers On Wheelchairs Folding Lightweight wheelchairs folding lightweight – https://heavenarticle.Com

  11. 7 Simple Strategies To Totally Intoxicating Your Citroen Key
    citroen key fob

  12. Diplomi_bdkn表示:

    диплом переквалификация купить 1russa-diploms.ru .

  13. bitcoin表示:

    Neat blog! Is your theme custom made or did you download it from somewhere?

    A theme like yours with a few simple adjustements would really make
    my blog shine. Please let me know where you got your design. Cheers

  14. Thіs design iѕ steller! Yoᥙ certainly know һow to keep a reader
    entertained. Between your wit and your videos, I was alm᧐st moved to start myy ߋwn blog (wеll, almߋst…HaHa!) Fantastioc job.
    І rеally loved ᴡhat you had tо sаү, ɑnd more than thаt, how yoou presentеd it.
    Tooo cool!

    Visit mү homepagе: Rajabandot Link

  15. This text is invaluable. How can I find out more?

    https://w7.liveresult.best/

  16. Kelvin表示:

    5 Clarifications Regarding Cost Replacement Car Key cheapest replacement
    car keys (Kelvin)

  17. fuck at mall表示:

    wonderful post, very informative. I wonder why the other experts of this sector
    do not realize this. You should continue your writing.
    I’m confident, you have a huge readers’ base already!

  18. Тут можно преобрести сейф пожаростойкий купить огнестойкий сейф в москве

  19. If you would like to grow your experience only keep visiting this website
    and be updated with the newest news update posted here.

  20. Diplomi_iokn表示:

    гознак аттестаты купить гознак аттестаты купить .

  21. pg slot表示:

    I every time spent my half an hour to read this website’s articles
    or reviews all the time along with a cup of coffee. pg slot

  22. When I initially commented I clicked the “Notify me when new comments are added” checkbox and now each time
    a comment is added I get three emails with the same comment.

    Is there any way you can remove people from that service?
    Thanks!

  23. Drug prescribing information. Effects of Drug Abuse.
    order generic fexofenadine pill
    Some what you want to know about drug. Get information now.

  24. Derekfar表示:

    slot siteleri: slot siteleri – slot oyunlar?

發佈留言

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