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,520,502 Responses

  1. 15 Top Pinterest Boards From All Time About Volkswagen Key Programming
    volkswagen Key Copy

  2. На украинский рынок это интернет https://1win-pas-official2.top/ вышло в 2021 году.

  3. See What Best Bunk Bed Brands Tricks The Celebs Are Making Use Of best bunk bed Brands

  4. Wow! In the end I got a web site from where I
    be able to genuinely take valuable information concerning my study and knowledge.

  5. The Unspoken Secrets Of Volkswagen Key Replacement Volkswagen key maker

  6. I’m really enjoying the theme/design of your blog.
    Do you ever run into any internet browser compatibility problems?
    A number of my blog visitors have complained about my
    site not operating correctly in Explorer but looks great in Firefox.

    Do you have any suggestions to help fix this problem?

  7. ốc mỡ表示:

    Just wish to say your article is as astounding. The clearness to your submit is simply great and i can assume you are knowledgeable in this subject.
    Fine along with your permission let me to grab your RSS feed to keep
    updated with forthcoming post. Thank you 1,000,000 and please continue the gratifying work.

  8. 5 Lessons You Can Learn From Best Value Bean To Cup Coffee Machine best bean to cup coffee beans (https://www.valeriarp.com.Tr/index.php?action=profile;u=231275)

  9. Medik.Co.Kr表示:

    9 Things Your Parents Teach You About Repairing Bottom Of Bifold Door Repairing Bottom Of Bifold Door (Medik.Co.Kr)

  10. 10 Things That Your Family Teach You About Daftar Akun Togel Resmi daftar akun Togel Resmi

  11. 10 Wrong Answers To Common Robot Vacuum And Mop Pet
    Hair Questions Do You Know The Right Ones? best robot vacuum and mop for pet
    hair; http://Www.otyjob.com,

  12. 9 Signs You’re A Robot Vacuums For Pet Hair Expert Best Robot Vacuum For Large House

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

    Here is my blog post provadent doctor

  14. zwoltek01表示:

    Hello There. I found your blog using msn. This is a very well written article.
    I will make sure to bookmark it and return to read more
    of your useful information. Thanks for the post. I’ll definitely comeback.

    Here is my page; zwoltek01

  15. Darrelherse表示:

    slot siteleri: deneme bonusu veren siteler – oyun siteleri slot

  16. The 10 Most Terrifying Things About 3 Seater Chaise 3 Seater chaise, https://www.metasoa.com/,

  17. The Reasons To Focus On Improving ADD Treatment For Adults severe untreated Adhd in adults

  18. выведение из запоя нижний новгород выведение из запоя нижний новгород .

  19. капельница от запоя на дому нижний новгород капельница от запоя на дому нижний новгород .

  20. evisa vietnam表示:

    Hello there, I do think your site could be having browser compatibility issues.
    When I look at your site in Safari, it looks fine
    however when opening in I.E., it has some overlapping issues.
    I merely wanted to give you a quick heads up! Apart from that, excellent website!

  21. I’m not sure why but this blog is loading extremely slow for me.

    Is anyone else having this issue or is it a problem on my end?

    I’ll check back later on and see if the problem still exists.

  22. Greetings! I know this is kinda off topic but I was wondering which blog platform are you using for this website?
    I’m getting tired of WordPress because I’ve had problems
    with hackers and I’m looking at alternatives for another platform.
    I would be fantastic if you could point me in the direction of a good
    platform.

    my web page … does tonic greens cure hsv2

  23. mobile表示:

    Do Not Buy Into These “Trends” Concerning Car Locksmiths Near Me mobile

  24. Guide To Best Car Seat For Newborn: The Intermediate Guide
    To Best Car Seat For Newborn Best car Seat for newborn

  25. You’ll Be Unable To Guess Long Couch With Chaise’s Tricks Couch With Chaise

  26. KevinSounc表示:

    https://sweetbonanza.network/# sweet bonanza guncel

  27. The Reasons Self Empty Robot Vacuum And Mop Is Everywhere This Year best robot vacuum and mop self cleaning

  28. Darrelherse表示:

    slot kumar siteleri: slot kumar siteleri – deneme veren slot siteleri

發佈留言

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