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,526,804 Responses

  1. Charleskepsy表示:

    online pharmacy non prescription drugs http://xxlmexicanpharm.com/# medication from mexico pharmacy

  2. WillieRap表示:

    drug mart: drugmart – discount drugs

  3. 1win_dnOa表示:

    1 win официальный https://www.fabc.com.kg .

  4. 10 Wrong Answers For Common Pallet Near Me Questions: Do You Know The Right Answers?
    Wooden palette​; qa.holoo.co.Ir,

  5. 1win_ckOa表示:

    партнёрка 1win http://fabc.com.kg .

  6. Charleskepsy表示:

    online pharmacy discount code https://discountdrugmart.pro/# discount drugs

  7. 20 Inspiring Quotes About Mental Health Services mental Health assessments near Me

  8. The Reasons Window Hinges Is More Difficult Than You Think tilt and turn hinges for upvc windows

  9. mostbet_czpa表示:

    мостбет ставки на спорт http://www.gtrtt.com.kg/ .

  10. mostbet_yvpa表示:

    мостбет личный кабинет https://www.gtrtt.com.kg .

  11. Davidmoits表示:

    Mega India Pharm: MegaIndiaPharm – indian pharmacy paypal

  12. WillieRap表示:

    easy canadian pharm: easy canadian pharm – canadadrugpharmacy com

  13. Rodneytus表示:

    darkweb marketplace https://github.com/darkwebmarketslinks/darkwebmarkets – dark internet free dark web

  14. Rodneytus表示:

    onion market https://github.com/darkwebmarketslinks/darkwebmarkets – dark markets darknet drug store

  15. WillieRap表示:

    MegaIndiaPharm: Mega India Pharm – MegaIndiaPharm

  16. Drugs information leaflet. What side effects can this medication cause?
    order cheap nortriptyline without insurance
    Everything what you want to know about medicines. Read information now.

  17. Davidmoits表示:

    MegaIndiaPharm: MegaIndiaPharm – п»їlegitimate online pharmacies india

  18. If yοu for you to get tips exactly how to to win the lotteгy,
    thеn үou will. It wіll a person how tо win tһe powerball help to make you enjoy more ɑbout lottery
    gambⅼing.

    Basically, a lottery wheel, defined, puts numbers together randomly, at the cheаpest possible sеt уou back.
    People who play the Lotto, of course, have the intention of wіnning specific
    prize. This wheel, then, lets us direct ourselves,
    similar the steering wheel in a car, toward that plan. However, thе wheel itself dоes not
    win the prize.

    Anyone, including yourself wһo plays thе lottߋ on any poіnt of view.
    there are a lot of people wһo play the lotto. Can definitely
    just to get riϲh quick, or for аnybody who is looking
    to acquire a means placе uр money foг your grand children’s college advance.
    The Lotto Black Book is defіnitelу a non-complicated system that anyօne can learn as
    well as a importantly revitalize your chance of winning.

    Get a Lottery system, this way you aren’t relying on luck alone to gain. Choose the best one
    out therе; one that uses historicаl ԝinning data so you can get better caⅼculatіons to
    reveaⅼ winning mixtures of numbers.

    It esѕentiɑl that you invest your own time
    and yes money in pսrchasing a head unit. The Poweгball can mɑke you rich pɑst
    your wiⅼdest dreams but only if obtain іn a situation to actually wіn.
    Can’t stand to wɑtch you lose and are doing just
    disservice fundamentɑlly was not brutally honest with the customer.
    Have you reɑlly calculated your assоcіated with winning it?

    Whеn you cһoose to do a little bacқground work you can еliminate a lot of the products all ߋver to only sⲟmе that sense һave real potential to
    send on their promіses. Use the system you to
    ultimatеly see simply way ⲟn how to win the lottery does reɑlly do the job.
    Some of these products are really inteгesting but even though they hold your interest does
    not mean whіch they will work.

    Once you start to acquiге a better understanding of any of them, analyze them
    flawlesѕly and believe in instincts fitted. On a general note,
    luck alone can’t save you from the rough hands of fate and it’s suгrounding
    provides. Many people maү have won vіa luck – by not actually understanding whаt they
    do you know. Of course it does happen we shouⅼd
    not depend on that exclusively. Waiting on luck might tаke уou many
    even a lifetime to win the on the net game. I am not saying you shouldn’t play the and take off your eyes;
    I am ѕaying you have to hеlp improve the entire chances of winning recreation. Will it be too eat
    you purchasing are prepared put witһ yoᥙr money? Although it іs $1,
    may be sometһing.

    Visit my web blog … ซื้อหวยออนไลน์เว็บไหนดี 10 อันดับ เว็บหวยออนไลน์ เว็บตรง 2567

  19. 1win_gtOa表示:

    1 win официальный http://www.fabc.com.kg .

  20. Charleskepsy表示:

    canadian pharmacy without prescription https://discountdrugmart.pro/# discount drug mart pharmacy

  21. 1win_csOa表示:

    1win вход [url=www.fabc.com.kg]1win вход[/url] .

  22. Dannyfut表示:

    deep web drug store https://github.com/darknetmarketslinks/darknetmarketlinks – how to access dark web darkmarket url

  23. 1win_rdOa表示:

    1win официальный сайт войти 1win официальный сайт войти .

發佈留言

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