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,608,269 Responses

  1. LeroyBak表示:

    best non prescription ed pills low cost pharmacy ed help

  2. The No. 1 Question Everyone Working In Mental Illness Assessment Should Know How To Answer Mental Health Disorder Assessment

  3. Hi, i believe that i noticed you visited my site thus i came to return the choose?.I am trying
    to to find issues to enhance my site!I assume its good enough to make
    use of a few of your ideas!!

  4. LeroyBak表示:

    mexican pharmaceuticals online mexican pharmacy purple pharmacy mexico price list

  5. Williamagric表示:

    ed cure https://indianpharm24.pro/# online shopping pharmacy india

  6. Jorgeagopy表示:

    cheapest online pharmacy india: indian pharmacy purchase online – world pharmacy india

  7. Sportstar, Crew (6 January 2019).

  8. I’m gone to inform my little brother, that he should also go to
    see this web site on regular basis to take updated from
    most recent reports.

  9. Treadmill With Incline Of 12 Tips From The Most Successful In The Industry Small Treadmill
    incline (https://js3g.com)

  10. Williamagric表示:

    do i have ed http://indianpharm24.pro/# online shopping pharmacy india

  11. LeroyBak表示:

    medication from mexico pharmacy mexico pharmacy cheap reputable mexican pharmacies online

  12. It’s an awesome article in favor of all the internet visitors;
    they will get benefit from it I am sure.

  13. Hello every one, here every person is sharing such experience, therefore it’s fastidious to read this
    website, and I used to go to see this blog daily.

  14. slot gacor表示:

    I am regular visitor, how are you everybody? This paragraph posted at this web site is in fact
    nice.

  15. Use large needle to make holes within the middle top of the helmet.

  16. DevonPhack表示:

    https://indianpharm24.pro/# buy prescription drugs from india
    comfortis without vet prescription

  17. Raymond表示:

    The Top Key Repair Gurus Are Doing 3 Things repair car keys near me
    Raymond

  18. Howdy just wanted to give you a brief heads up and let you know a few of the pictures aren’t loading properly.
    I’m not sure why but I think its a linking issue.
    I’ve tried it in two different browsers and both show the
    same results.

  19. Pretty element of content. I simply stumbled upon your site and in accession capital to assert that I acquire actually loved account your weblog posts.
    Any way I will be subscribing to your augment and even I achievement you access consistently rapidly.

  20. A Peek Inside Foldable Treadmill With Incline’s Secrets Of Foldable Treadmill With
    Incline Folding Treadmills

  21. Jarred表示:

    The Unspoken Secrets Of Adult Adhd Assessment Uk why are adhd assessments so expensive (Jarred)

  22. Good post however I was wanting to know if you could write a litte more on this
    topic? I’d be very thankful if you could elaborate a little
    bit further. Many thanks!

  23. LeroyBak表示:

    indian pharmacy paypal Indian pharmacy to USA online pharmacy india

  24. What’s up, the whole thing is going well here and ofcourse every one
    is sharing information, that’s truly excellent, keep up writing.

  25. 10 Life Lessons We Can Learn From Volvo Excavator Key Volvo xc60 key fob
    functions (Opensourcebridge.science)

  26. LeroyBak表示:

    viagra without a doctor prescription cheap drugs ed treatments

  27. ส้ม777表示:

    Most of fօlks believe that winning a lottery
    is entirely inside of the hands of Lady Luck-the goddess
    of fortune. She’ѕ the ultimate and truly the only entity to determine whether must win the lottery or not.
    But, aсtion not reаlly true. Winning a lottery also will
    ⅼie upon you. If you play smartly and use certain strategies, you can maⲭimise your odds of of wooing.

    Every Lotto game worldwiԁe һas which arе coming out day.

    As well as that the prizes have jackpotted to grow to be the biggest on tomorrow.
    Or it could ƅe per that many of us һave free in order to ⲣurchase a priced.
    foг exɑmple a Saturday. Here’s do not.

    The Powerball third prizе odds are 723,145 to just one with a payout of $10,000.
    ᒪaѕt and fifth leveⅼ prize odԀs are 19,031 and
    13,645 respectiveⅼy with a payout of $100 each. Mega Millions’ third prize itrrrs liҝely that 89,065 to1 with a payout of $10,000.
    Last and fifth prize level odԀs arе 15,313 and 13,781 respectively with a payout of $150
    both of those.

    There are some practical strategies that you can employ to easily win any American Lotto game.
    Youг current products use these strategies aѕ part of your
    advantage, your chanceѕ of wіnning the lotto will grеаtly improve in order to some point of being a guaranteed winner.

    It’s worth pointing out when using Lottery Wheels they carry
    some regarding guarаntee. Perһɑpѕ it is a 5
    from 6 which means if 6 winning numbers are with your selections a
    new guarantеe normally at least 5 numbers will fall together.
    You need to ɡet other prizes also.

    Powerball involving 59 white ƅalls, ɑnd there
    is no law that forbids in order to definitely play aⅼl һighs several lows.
    Βսt think of this: only 4% amongst all the Powerball dгaws consists of entirely high аnd entirely
    low winning numbers. Plans ready like to bet those numЬers
    that are sentіmental or meaningful to them, for instance date that Baby Jоhn first said ‘Papa’ or when Tina and Brad decided to obtain married, or
    possibly simply the date that he was crɑfted. Thеre is nothing wrong with a form of that, but keep from
    heart that the datеs from a calendar only makes
    uр 31, and also the Powerball is well high over by investing in 59.

    This taƄle says everything. You can see that it took two or more Longshots to win the lottery jackpоt, in 67% of the drawings.
    Witһout knowing New York Lotto’s dirty little secret, you were beating a defunct horse.

    my ѡeb site; ส้ม777

發佈留言

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