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,370,345 Responses

  1. RobertJar表示:

    amoxicillin script: generic amoxicillin over the counter – amoxicillin 500 mg purchase without prescription

  2. Medication information. Long-Term Effects.
    can i order rizatriptan without prescription
    Everything trends of medicines. Get information here.

  3. Jerry Ortiz表示:

    Authority link building is actually critical for boosting your website’s integrity and search engine rankings buy backlinks

  4. The future belongs to those who can think critically, adapt quickly, and solve complex problems Ρομποτική για παιδιά

  5. DanielAbice表示:

    https://clomidonpharm.com/# how to buy generic clomid without rx

  6. African Grey Parrot Tools To Ease Your Daily Life African Grey
    Parrot Trick That Should Be Used By Everyone Learn african grey parrot

  7. SamuelBix表示:

    where can you get amoxicillin: amoxicillin without a prescription – amoxicillin 500 mg purchase without prescription

  8. SamuelBix表示:

    amoxicillin 500 mg cost: generic amoxil 500 mg – amoxicillin no prescipion

  9. Hey I know this is off topic but I was wondering if you knew of any
    widgets I could add to my blog that automatically tweet my newest twitter updates.
    I’ve been looking for a plug-in like this for quite some time and
    was hoping maybe you would have some experience with something like this.
    Please let me know if you run into anything.
    I truly enjoy reading your blog and I look forward to your new updates.

  10. If you are going for most excellent contents like myself, simply visit this web page everyday since it presents feature contents, thanks

    Have a look at my webpage: what is alpha bites

  11. SamuelBix表示:

    prednisone 60 mg: PredniBest – can you buy prednisone over the counter

  12. WOW just what I was looking for. Came here by searching for tiktok beğeni hilesi

  13. sga123表示:

    I’m gone to inform my little brother, that he should also visit this web site on regular basis to obtain updated from most up-to-date news.

  14. EdgarWat表示:

    Интересные статьи о займах – https://www.r-nk.com/sravni-ru-osobennosti-vybora-kreditora.html

  15. I wish I had known about the importance of a best personal injury attorney sooner

  16. Nikefut表示:

    darknet market list dark web market dark web sites

  17. вывод из запоя в санкт-петербурге [url=belbeer.borda.ru/?1-6-0-00000780-000-0-0-1730825410]вывод из запоя в санкт-петербурге[/url] .

  18. Just had my first experience in an old-school barbershop—it was fantastic! Explore that vibe further at barber shop θεσσαλονικη

  19. KennethPah表示:

    where to buy amoxicillin 500mg without prescription Amox Star where can you buy amoxicillin over the counter

  20. Aw, this was a really nice post. Taking the time and actual effort to make a great article…
    but what can I say… I procrastinate a whole lot and never
    seem to get nearly anything done.

  21. Very great post. I simply stumbled upon your blog and wanted
    to say that I have really loved browsing your weblog posts.
    After all I’ll be subscribing on your feed and I am hoping you write
    again soon!

    my site … alpha bites gummies

  22. car transport表示:

    This post is really a nice one it helps new internet visitors,
    who are wishing for blogging.

  23. Dominic表示:

    10 No-Fuss Methods For Figuring Out Your SEO Tools seo powersuite date ranges
    for reports (Dominic)

  24. cordarone blood glucose levels nexterone vs cordarone cordarone pronunciation key
    cost of cordarone without insurance lements cordarone cordarone pacerone side effects
    cost generic cordarone pills
    cordarone blood glucose levels cordarone 200 mg canada fortress supplements cordarone uses
    cordarone maintenance dose cordarone 100 mg side effects cordarone effetti collaterali

  25. WOW just what I was searching for. Came here by searching for sex indo viral

發佈留言

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