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,366,577 Responses

  1. key表示:

    Five Killer Quora Answers To Audi A3 Key Replacement key

  2. Ronaldpsymn表示:

    http://lisinopril.network/# lisinopril price comparison

  3. RandallTaulp表示:

    hysterectomy after breast cancer tamoxifen: tamoxifen rash – tamoxifen lawsuit

  4. Важно! если хотите выращивать плодовые деревья, verin-sad.ru то останавливайтесь низкорослым породам.

    Feel free to surf to my web blog: https://oferme.ru/

  5. 20 Things That Only The Most Devoted Treatments Of Adhd Fans Should Know adult adhd treatment uk

  6. Who Is The World’s Top Expert On Cheapest Butt Plugs? App Controlled
    Buttplug (https://Menwiki.Men/)

  7. 10 Of The Top Mobile Apps To Mesothelioma mesothelioma attorney

  8. 5 Injury Lawyer Lessons From Professionals Injury lawsuits

  9. RandallTaulp表示:

    zestril 30 mg: prinivil generic – lisinopril 5 mg tablet

  10. What Is The Reason Mobile Diagnostics Is The Right Choice For You?
    diagnostics car

  11. Source表示:

    It’s appropriate time to make a few plans for the future and
    it’s time to be happy. I’ve read this put up and if I could I
    wish to suggest you some fascinating things or advice.
    Perhaps you could write subsequent articles
    referring to this article. I want to read more issues about it!

  12. How Online Home Based Work Rose To The #1 Trend On Social Media work from home no experience

  13. WilliamRhype表示:

    https://finasteride.store/# cost propecia without a prescription

  14. RandallTaulp表示:

    buy cytotec pills: cytotec pills online – Abortion pills online

  15. Gregorio表示:

    See What Mobility Scooter Automatic Folding Tricks The Celebs Are Utilizing mobility scooter automatic folding (Gregorio)

  16. Athena表示:

    This Is The Ultimate Guide To How Do You Unlock A Car Without A Key locksmith
    opening car door; Athena,

  17. WilliamRhype表示:

    http://nolvadex.life/# tamoxifen benefits

  18. 5 Killer Quora Questions On Childrens Bunk Bed modern bunk Beds

  19. A Trip Back In Time A Trip Back In Time:
    What People Talked About Repairs To Upvc Windows 20
    Years Ago Window Repairs

  20. Could Window Repair Near Be The Key For 2023’s Challenges?
    window repair Near Me

  21. glass表示:

    10 Facts About Window Repair That Will Instantly Make You Feel Good Mood glass

  22. 15 Things You Didn’t Know About 18 Wheeler Accident Law Firm 18 wheeler Accidents

  23. Every weekend i used to pay a quick visit this web page, for the reason that i wish for
    enjoyment, for the reason that this this web site conations really pleasant fuynny material too.

    Also visit my website – Aizen Power Reviews

  24. Ronaldpsymn表示:

    https://cytotec.club/# buy cytotec over the counter

  25. 15 Twitter Accounts That Are The Best To Find Out
    More About Door Fitters Leeds window doctor leeds

  26. Leeds Glazing表示:

    5 Double Glazed Window Leeds Projects For Any Budget Leeds Glazing

  27. WilliamRhype表示:

    http://nolvadex.life/# tamoxifen for gynecomastia reviews

  28. Double Glazing Companies Near Me Tips To Relax Your Daily
    Life Double Glazing Companies Near Me Trick
    Every Individual Should Learn Double Glazing Companies Near Me

發佈留言

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