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,498,777 Responses

  1. What’s up, I desire to subscribe for this blog to take hottest updates, thus where can i do it please assist.

  2. Nilda表示:

    Fridge Freezer With Ice And Water Dispenser: What No One Is
    Talking About refrigerator water cooler (Nilda)

  3. Why Getting An ADHD Diagnosis Is Fast Becoming The Most Popular
    Trend In 2023 Adult Adhd Diagnosis northern ireland

  4. 10 Things You Learned In Kindergarden To Help You Get
    Started With Mesothelioma Attorney Mesothelioma Claim

  5. The Reason Treadmill Foldable Electric Is So Beneficial
    When COVID-19 Is In Session Folding Electric Treadmill Uk

  6. Ten Double Pushchairs That Really Change Your Life Double pushchairs

  7. See What Experienced Birth Injury Lawyers Tricks The Celebs Are Using Experienced Birth Injury Lawyers

  8. Darrelherse表示:

    en iyi slot siteleri: slot oyunlar? siteleri – slot siteleri bonus veren

  9. Loreen表示:

    Please let me know if you’re looking for a article author for your site.
    You have some really good articles and I believe I would
    be a good asset. If you ever want to take some
    of the load off, I’d absolutely love to write some material for your blog in exchange
    for a link back to mine. Please shoot me an email if interested.
    Many thanks!

  10. Highly descriptive post, I loved that a lot. Will there be a
    part 2?

  11. Darrelherse表示:

    bonus veren slot siteleri: en cok kazandiran slot siteleri – slot bahis siteleri

  12. How You Can Use A Weekly Electric Fireplace Wall Mounted
    Project Can Change Your Life modern fireplace

  13. Great blog here! Also your website loads up fast!
    What host are you using? Can I get your affiliate link
    to your host? I wish my website loaded up as fast as yours lol

  14. Doug表示:

    It’s a pity you don’t have a donate button! I’d without a doubt donate to this fantastic blog!
    I suppose for now i’ll settle for book-marking and adding your
    RSS feed to my Google account. I look forward to fresh updates and will talk about this site with my Facebook group.
    Chat soon!

  15. You’ll Be Unable To Guess Treadmill Incline Workout’s Tricks
    Treadmill incline workout

  16. Darrelherse表示:

    slot siteleri 2024: guvenilir slot siteleri 2024 – bonus veren slot siteleri

  17. Nice blog here! Also your web site loads up fast! What
    host are you using? Can I get your affiliate link
    to your host? I wish my web site loaded up as fast as yours lol

  18. You’ll Never Be Able To Figure Out This Couch L Shaped Sofa’s Secrets Couch l shaped Sofa

  19. What’s The Job Market For Treadmill Shop Near Me Professionals Like?

    Treadmill Shop Near Me

  20. Darrelherse表示:

    slot oyunlar? siteleri: slot siteleri guvenilir – slot siteleri 2024

  21. Thanks , I’ve recently been searching for information approximately this topic for a while and yours is the best I’ve came
    upon till now. However, what concerning the conclusion? Are you positive in regards to the supply?

  22. Darrelherse表示:

    en iyi slot siteler: slot oyunlar? siteleri – en guvenilir slot siteleri

  23. 15 Funny People Who Are Secretly Working In Washington Mesothelioma Lawyer Mesothelioma lawyer ca

  24. I’m not that much of a internet reader to be honest but your blogs
    really nice, keep it up! I’ll go ahead and bookmark your site to
    come back later. Cheers

  25. 5 Killer Qora’s Answers To Volvo C30 Key volvo v40 key programming

  26. Darrelherse表示:

    deneme bonusu veren slot siteleri: slot oyun siteleri – slot bahis siteleri

  27. Darrelherse表示:

    deneme bonusu veren slot siteleri: yeni slot siteleri – casino slot siteleri

  28. fantastic publish, very informative. I ponder why the opposite
    experts of this sector don’t realize this.
    You should proceed your writing. I’m sure, you’ve a huge readers’ base already!

  29. remote表示:

    Do You Think You’re Suited For Doing Volvo Keyfob?

    Take This Quiz remote

  30. Pay Attention: Watch Out For How ADHD Diagnosis Private UK
    Is Taking Over And How To Stop It private adhd Assessment Ipswich

發佈留言

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