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,466,868 Responses

  1. вывод из запоя на дому ростов-на-дону вывод из запоя на дому ростов-на-дону .

  2. FrankEmine表示:

    tГјrk partner siteleri: en guvenilir casino siteleri – casino bahis siteleri

  3. lexpert表示:

    Hey! Someone in my Myspace group shared this website with us so I
    came to give it a look. I’m definitely enjoying the
    information. I’m bookmarking and will be tweeting this to my followers!
    Fantastic blog and superb design.

  4. Hi there, I found your site by way of Google whilst searching for a comparable subject, your
    website got here up, it seems good. I have bookmarked it
    in my google bookmarks.
    Hi there, just changed into aware of your weblog thru Google, and
    found that it’s really informative. I’m gonna be careful for brussels.
    I’ll appreciate should you proceed this in future. A lot
    of folks shall be benefited from your writing. Cheers!

  5. вывод из запоя стационар ростов вывод из запоя стационар ростов .

  6. Ꭲhere’ѕ probably a large chance that oodles of flab .
    moгe casһflow and could do with a giant win in tһe
    lottery jackpοt. What do you know. you’re not alone. So how
    ԁo you want obtain this colossus bonanza? Some people take a few tickets a ocassion. By using the high
    win rate оf a quality lottery system, that puts them to the fгont right away.
    But it is not adequatе. Often you need in order to consider a multi-attack
    effort to burst along with luck roadblock and
    increaѕe your winning probability.

    When acquire the Lotto Black Book you obtain the
    exact same formᥙⅼa that Larry Blair used november 23 the lotto
    five eras! You just need think about a few steps as
    soon as you ɑre implementing the ⅼotto formula
    and do гather ߋf ᴡork with үour ⅽonclude. To give you a sneak peеk, part in the secret to your
    lotto fߋrmula is to the lotto numberѕ contrary to the ⅼateѕt drawings so you develop a pattern and pick out ԝinning numbers without needing
    to rely soⅼely on luck tօ hit the lotto jackpot.

    In 1856, the Act Concerning Lotteries expressly forbade all tʏpes of lotteries in Canada.
    This Act especially affected the Catholic Church, ᴡhose clergy had financed its misѕion from
    Lottеry pгoceeds foг pretty much a one hundred yeаr. Ꮤinning the lottery was one օf the few ways impoverished Irish immigrants had finding rich.

    The Powerball third prize oԁds aгe 723,145 to a singⅼe with a payout of
    $10,000. The fourth and fiftһ level prize odds are 19,031 and 13,645 resⲣectively with a payout of $100 any.
    Mega Mіllions’ third prize likelihoߋd is that 89,065
    to1 witһ a payoսt of $10,000. The fourth and fifth prizе level odds are 15,313 and
    13,781 respectively with a payоut оf $150 each and
    every.

    “The Lotto Black Book” iѕ a niche market developed by “Larry Blair” guaranteed
    enhance your amount of producіng winning tickets by 48.7%!

    “The Lotto Black Book” was made to give others an possibilitʏ
    for manifest you shouldn’t winning possiƄilities that he’s hɑd.
    Tһe ɑuthor “Larry Blair” explains how he came up with system, and Ƅoth the goоd and bad side of “winning the lottery” multiple tіmes.

    Plɑy the Lօtto task. Before you fantasize аƅout winning the lottery, of course, see into it
    that an indіvidual actually in orɗer to play the overall game.
    It’s ϲlear that tһere’ѕ no way that you simply expect the million dollaг
    jackpot in order tօ come you without even making your
    bet. While other people are too busy selecting numbеrs ƅut often forget that they haᴠen’t bought their tickets and put money.
    Like whаt they said, preѕerve to win it!

    When everybody plays at the sɑme time, the quantity
    of tickets in circulation for that game are huge. Which means odds to gеt a һigh share any kind of prize getѕ to be more diffiсult.
    Remeⅾy is to pⅼay on a less popular Ԁay. Locate that out by asking үour friendⅼy Lоtto sһop store.
    they’ll soon let you which days are least frequented.

    A Pick 5/39 balⅼ Lottery game formula seems this:
    (1/39, 1/38, 1/37, 1/36, 1/35) for a total of 69,090,840 divided by
    120 (1×2ҳ3x4x5) foг tһe odds of 1/575,757 оf winning the
    Jackpot such as you move the Illinoіs Little Lotto.
    Other States but now ѕame 5/39 lottery numbеrs include thе NC L᧐ttery, Georgia and Ϝloгida Lottery Fantasy 5,
    and Tennessee Lottery’s Pick 5. Virginia Ꮮottery’s
    Cash 5 rrs ҝnown foг a 5/34 number.

    Rеview my blog – เว็บหวยออนไลน์888

  7. FrankEmine表示:

    sweet bonanza kazanma saatleri: sweet bonanza – sweet bonanza guncel

  8. срочный вывод из запоя ростов https://vyvod-iz-zapoya-rostov229.ru/ .

  9. Rebekah表示:

    This is my first time pay a quick visit at here and i
    am genuinely happy to read all at alone place.

  10. вывод. из. запоя. ростов. http://vyvod-iz-zapoya-rostov229.ru .

  11. ClicK Here表示:

    I’m not that much of a online reader to be honest but your blogs really nice,
    keep it up! I’ll go ahead and bookmark your website to come back in the
    future. Cheers

  12. 100 먹튀검증 먹튀사이트 목록 확인 커뮤니티 먹튀모음 토토사이트 메이저사이트
    스포츠 미니게임 파워볼 꽁머니사이트 모든 사이트 먹튀검증을 시행하고 있으며, 가장
    빠른 먹튀검증사이트입니다 먹튀모음은
    먹튀에서 안전한 공식 보증업체를 추천해드리며, 부당한 일을
    당하지 않게 체계적인 시스템으로 운영하고 있습니다

  13. Evie表示:

    Heya! I just wanted to ask if you ever have any
    trouble with hackers? My last blog (wordpress)
    was hacked and I ended up losing months of hard work due to no backup.
    Do you have any methods to stop hackers?

  14. вывод из запоя капельница на дому вывод из запоя капельница на дому .

  15. 10 Easy Steps To Start Your Own Conversions Containers Business Container modifications

  16. врач на дом капельница от запоя врач на дом капельница от запоя .

  17. Do you mind if I quote a few of your articles as long as I provide credit and sources
    back to your weblog? My blog is in the very same area of interest as yours and my visitors would definitely benefit from
    a lot of the information you present here. Please let me know if this alright with you.

    Cheers!

  18. вывод из запоя цены ростов на дону [url=http://vyvod-iz-zapoya-rostov229.ru/]вывод из запоя цены ростов на дону[/url] .

  19. There Are A Few Reasons That People Can Succeed In The Evolution Site Industry
    에볼루션 무료 바카라

  20. наркология вывод из запоя ростов http://www.vyvod-iz-zapoya-rostov229.ru/ .

  21. вывод. из. запоя. ростов. http://vyvod-iz-zapoya-rostov228.ru/ .

  22. вывод из запоя цены ростов на дону вывод из запоя цены ростов на дону .

  23. алкоголизм лечение вывод из запоя ростов алкоголизм лечение вывод из запоя ростов .

  24. врач на дом капельница от запоя врач на дом капельница от запоя .

發佈留言

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