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,153,956 Responses

  1. The Most Hilarious Complaints We’ve Received About Leather Sofa
    reupholster Leather Couch

  2. 20 Great Tweets From All Time About ADHD Untreated In Adults Symptoms Of Adhd In Adults Treatment

  3. Great work! That is the kind of info that should be shared
    around the internet. Shame on Google for no longer positioning this put up upper!
    Come on over and visit my website . Thank you =)

  4. Are You Responsible For A ADHD Private Assessment Budget?
    10 Unfortunate Ways To Spend Your Money Adhd Assessment Private uk

  5. BomesJoype表示:

    Lorem Ipsum has been the industry’s standard dummy text https://breakoutseason.es/wllqvl of type and scrambled it to make a type specimen book.

  6. Ten 18 Wheeler Accident Law Firms That Really Change Your Life 18 wheeler
    accident law firms (m.www.towelking.co.kr)

  7. Subscribers who sign multiple contracts receive more capital or control over key functions of the https://blast.io-event.network/mainnet/ protocol in the main network.

  8. cassylawn.top表示:

    Don’t Buy Into These “Trends” About Asbestos Attorney
    Mesothelioma cassylawn.top

  9. You’ll Be Unable To Guess Online Shopping Uk Sites’s Benefits Online shopping uk sites

  10. 15 Gifts For The Slot Promotions Lover In Your Life
    exciting casino slots

  11. bokep viral表示:

    Your method of describing everything in this article is really fastidious, every one can easily be aware of it, Thanks a lot.

  12. Dangerous Drugs Lawyers: It’s Not As Difficult As You Think Dangerous Drugs attorneys

  13. Danieldob表示:

    Приветствую. Подскажите, где найтиполезные блоги о недвижимости? Пока нашел https://universal37.ru

  14. Guide To Slot Payouts: The Intermediate Guide
    For Slot Payouts Slot payouts (m.Asdf.blackpeach.co.kr)

  15. I am not sure where you’re getting your information, but good topic.
    I needs to spend some time learning much more or understanding more.
    Thanks for excellent info I was looking for this info for my mission.

  16. Do Not Make This Blunder With Your Slot Wins video poker Slots

  17. Archie表示:

    Hi there mates, nice piece of writing and nice urging commented at this place,
    I am genuinely enjoying by these.

  18. youjizz表示:

    This is a great tip particularly to those new to the blogosphere.
    Short but very precise info… Many thanks
    for sharing this one. A must read article!

  19. zeus4d表示:

    It’s remarkable to go to see this web site and reading the views of all mates
    concerning this piece of writing, while I am also zealous
    of getting know-how.

  20. rsmegane.com表示:

    Interactive Slots Tools To Make Your Daily Life Interactive Slots Trick That Every Person Should Know Interactive slots (rsmegane.com)

  21. Pills information for patients. What side effects?
    how to buy cheap propecia no prescription
    Best about meds. Read now.

  22. vimeo表示:

    Could Best Online Shopping Sites Clothes Be The Answer To 2023’s Resolving?
    vimeo

發佈留言

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