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,374,136 Responses

  1. 10 Best Facebook Pages Of All Time Concerning Add In Adult Women Inattentive Adhd In Women

  2. I needed to thank you for this great read!! I absolutely enjoyed every little bit of it.
    I’ve got you book marked to look at new stuff you post…

  3. It’s Time To Expand Your Misted Up Glazing Repair Options Misty Double Glazing

  4. 10 Tell-Tale Warning Signs You Need To Get A New Best Squirting Dildos Strap on that Squirts

  5. It is appropriate time to make a few plans for the long
    run and it is time to be happy. I have learn this submit and if I may I wish to suggest you some interesting
    issues or advice. Maybe you can write subsequent articles relating to
    this article. I wish to read more issues about it!

  6. В магазине сейфов предлагают новый сейф сейф купить в москве

  7. auto表示:

    What Freud Can Teach Us About Key Programing auto

  8. вывод из запоя недорого ростов http://www.vyvod-iz-zapoya-rostov18.ru .

  9. The Ugly Reality About Togel Singapore togel4D (https://atomcraft.Ru/)

  10. Guide To Situs Togel Dan Slot Terpercaya: The Intermediate Guide Towards Situs Togel Dan Slot Terpercaya situs togel dan slot terpercaya

  11. Excellent post. Keep writing such kind of info on your page.

    Im really impressed by your site.
    Hello there, You have done an incredible job.
    I will definitely digg it and for my part recommend
    to my friends. I’m sure they will be benefited from this website.

  12. 3 Ways That The Treadmill Sale UK Will Influence Your Life treadmills For sale; telegra.ph,

  13. Judy Piquet表示:

    How To Make A Profitable Adhd In Females Symptoms Even If You’re Not Business-Savvy symptoms Of adhd in adults nhs (https://iampsychiatry70631.daneblogger.com/28469203/how-to-beat-your-boss-in-adhd-in-older-females-symptoms)

  14. Jeremypag表示:

    buy ozempic ozempic coupon buy ozempic pills online

  15. вывод из запоя цены ростов на дону https://vyvod-iz-zapoya-rostov18.ru .

  16. вызов нарколога на дом круглосуточно https://narkolog-na-dom-krasnodar15.ru/ .

  17. Werner King表示:

    Best Automatic Folding Mobility Scooter Uk Tools To Ease Your Daily Lifethe One Best Automatic Folding Mobility Scooter Uk Trick Every Individual Should Learn best automatic folding mobility Scooter Uk

  18. Keep on writing, great job!

  19. 20 Up-Andcomers To Watch The Toto Result Industry 토토사이트

  20. Best Automatic Folding Mobility Scooter Uk Tools To Help You Manage Your Everyday
    Lifethe Only Best Automatic Folding Mobility Scooter Uk Trick That Should Be Used By Everyone Learn best automatic Folding Mobility scooter

  21. анонимный вывод из запоя ростов анонимный вывод из запоя ростов .

  22. Great info. Lucky me I ran across your blog by accident (stumbleupon).
    I have bookmarked it for later!

    My website – lottery defeater software app

  23. You’ll Never Guess This Upvc Windows Birmingham’s Secrets Pvc Windows Birmingham [Willysforsale.Com]

  24. вывод из запоя цены ростов-на-дону http://vyvod-iz-zapoya-rostov18.ru .

  25. A fascinating discussion is definitely worth comment.
    I think that you need to write more about this topic, it might not be a taboo subject
    but generally people do not discuss such issues. To the next!

    Kind regards!!

  26. Arnulfo Wrenn表示:

    A Peek Inside Psychiatric Assessment Private’s Secrets Of Psychiatric Assessment Private how to Get psychiatric assessment

  27. RamonRek表示:

    ozempic cost: ozempic – ozempic online

  28. Gennie Ziemba表示:

    Nine Things That Your Parent Taught You About Double Glazing Repairs Near Me Double glazing repairs

發佈留言

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