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,366,206 Responses

  1. Hey there, You have done a great job. I’ll definitely digg it and personally suggest to my friends.
    I’m sure they will be benefited from this site.

  2. BarryDof表示:

    https://rybelsus.shop/# semaglutide online

  3. Josefa Loar表示:

    This Is The Kids Bunk Bed Case Study You’ll Never Forget Bunk Bed For Kids

  4. 11 “Faux Pas” Which Are Actually OK To Make With Your Upvc Doors Luton Windows and Doors Near me

  5. Your Worst Nightmare Concerning Retro Fridge Freezers Cream Get Real under counter retro fridge freezer,
    https://www.google.pt,

  6. Alba Rountree表示:

    You’ll Never Be Able To Figure Out This Best G Spot Toy’s Benefits best g spot Toy

  7. It’s The One Affordable SEO London Trick Every Person Should Learn Search Engine Optimization London

  8. Thomasanala表示:

    Услуги Riga escort предлагают идеальное сопровождение для вашего отдыха или деловой поездки https://www.escortinriga.lv

  9. Soustružení表示:

    You actually make it seem so easy with your presentation but I find this topic to be actually
    something which I think I would never understand.
    It seems too complicated and very broad for me. I am looking forward
    for your next post, I’ll try to get the hang of it!

  10. 5 Killer Quora Answers To Private ADHD Assessment UK Private Adhd Assessment Uk

  11. Hi to every body, it’s my first go to see of this web
    site; this web site carries amazing and actually
    good material designed for readers.

  12. Website表示:

    It’s a shame you don’t have a donate button! I’d definitely donate to this
    excellent blog! I suppose for now i’ll settle for bookmarking
    and adding your RSS feed to my Google account. I look forward to new updates and will share this website with my Facebook group.
    Chat soon!

  13. Jeremypag表示:

    semaglutide cost cheapest rybelsus pills semaglutide online

  14. Belen Peebles表示:

    15 Best Documentaries About Double-Ended Dildos Female Double Dildo

  15. Melvin Denham表示:

    7 Useful Tips For Making The Most Of Your Electric Folding Lightweight Wheelchair folding lightweight electric wheelchair – mobility-scooter85287.wikienlightenment.com,

  16. Find Out What Freestanding Bioethanol Fireplace The Celebs Are Using bio-ethanol fireplace (Maya)

  17. See What Cheapest Automatic Folding Mobility Scooter Tricks The Celebs Are Utilizing cheapest automatic folding mobility scooter (Charles)

  18. 14 Smart Ways To Spend Your Leftover Bristol Windows And Doors Budget upvc window and door repairs

  19. Frankevild表示:

    https://ozempic.art/# buy cheap ozempic

  20. Jeremypag表示:

    semaglutide online semaglutide cost semaglutide cost

  21. I do agree with all of the concepts you’ve presented for your
    post. They’re very convincing and will certainly work. Nonetheless,
    the posts are very quick for newbies. May just
    you please prolong them a little from subsequent time?
    Thanks for the post.

  22. Five Killer Quora Answers To Bio Ethanol Fireplaces ethanol fireplace (Darrell)

  23. Indira Law表示:

    9 Things Your Parents Teach You About Stroller 2 In 1 Car Seat Stroller 2 in 1 Car seat; olderworkers.Com.au,

  24. BarryDof表示:

    http://rybelsus.shop/# cheapest rybelsus pills

  25. Rocco Lundy表示:

    15 Things You Didn’t Know About Best French Door Fridge Freezer Uk French Door Retro Fridge

  26. BarryDof表示:

    http://rybelsus.shop/# rybelsus coupon

  27. This Week’s Top Stories About Wall-Mounted Fireplace Wall-Mounted Fireplace Wall mounted electric Fires uk (whitebookmarks.com)

  28. Search Engine Optimization Basics For Beginners – Learn Search
    Engine Seduction 백링크 구매

  29. What’s up to every , as I am truly keen of reading this weblog’s post to be updated
    regularly. It consists of good stuff.

  30. bolacasino88表示:

    Wow, this article is fastidious, my sister is analyzing
    such things, so I am going to convey her.

發佈留言

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