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,367,951 Responses

  1. BrandenSlomy表示:

    where buy generic clomid tablets: clomid online – can i get generic clomid without prescription

  2. BrandenSlomy表示:

    amoxicillin 500mg capsules uk: amoxil com pharm – 875 mg amoxicillin cost

  3. электрокарнизы для штор купить электрокарнизы для штор купить .

  4. cuaca778表示:

    My brother recommended I might like this website. He was totally right.
    This post truly made my day. You cann’t imagine simply how much
    time I had spent for this information! Thanks!

  5. Unquestionably believe that that you said. Your favourite justification seemed to
    be at the internet the simplest thing to bear in mind of.
    I say to you, I definitely get irked even as other people consider worries that they plainly do not know about.
    You managed to hit the nail upon the highest and also outlined out the whole thing without having side effect , other people can take a signal.
    Will likely be back to get more. Thank you

  6. StephenDeery表示:

    This is my first time visit at here and i am genuinely pleassant to read all at alone place.
    best online casino Australia

  7. cuaca778表示:

    Hello There. I found your blog using msn. This is a really well written article.

    I’ll be sure to bookmark it and return to read more of your
    useful information. Thanks for the post. I’ll definitely comeback.

  8. Medicament information leaflet. Long-Term Effects.
    duloxetine sigma
    Some about medicines. Read information now.

  9. StevenNub表示:

    amoxicillin 250 mg capsule: Com Pharm – amoxicillin 500 mg brand name

  10. Very nice post. I just stumbled upon your blog and wished to
    say that I have really enjoyed surfing around your blog posts.
    After all I’ll be subscribing to your rss feed and I hope you
    write again soon!

  11. Dereketerm表示:

    buy prednisone online from canada: online prednisone 5mg – prednisone uk buy

  12. StevenNub表示:

    order clomid without dr prescription: rexpharm – where can i buy cheap clomid price

  13. Nikefut表示:

    darknet search engine dark web link https://darknetmarketstore.com/ – deep web links

  14. ifin.gov.so表示:

    Tһere are not many things which are more exhilarating towаrds the
    first big win you obtain when betting sports ᴡithin tһe.

    It is а thrill tһat eclipsеs tһat mаny a
    vаriety of gambling, even offline sportѕ making
    a bet. There is nothing, short of an actual win at a casino’s sports book which may сompare wһilst experience.

    Free online slots are bɑsed on the same regulations and tactics
    actual slots. And, while should not ѕee thе same thrill as
    a proper casino, several probably be just as eⲭcited.
    Best of all, couⅼɗ ρossibly play previously comforts of one’s own hօusehold.
    That’s great news if suсh as playing witһ yoᥙr pajamas.

    The next thing in customer is gambling online. Online Casino
    s much more ϲonvenient than any local сasino, so no is actuaⅼly going to spread out up more detailed
    the risk taker. When an online casino iѕ losing business in aԁditіοn throw money at tһe issue.
    Online casinos do this by offering bonuѕes into the player, not with white tigers in the ⅼοbby.

    Winning is useful but forcing it to occur through underhanded
    means is cheap and bad. If yoս think you’re сomρetent at dirty pⅼay in your prіvate gambling table,
    don’t continue the haЬit to the casino or rіsk in order to be jɑil.

    Craps has become most popular Inteгnet Cɑsino Online games.
    In craps, players bet with the outcome оf tһis rolling of two cube.
    You can choose to ƅet on a variety of thіngs ᴡhich include specific
    rolⅼ, a regɑrding possiƅle rⲟlls, wһether or the rolling player will win most.
    The best strategy for winning at craps is to understand about ⅽraps oⅾds and to only place ƅets in the
    fieldѕ with highest winning probabilities. Bettіng fields from wheгe the house characterіstics
    low edge include ⲣass bets, come bets and also bets
    on 6 or 8. Уow will disсover craps odds and probability chaгtѕ ߋn the ѡebsite
    for and also complete varіety.

    Check out several different sіtes befоre deciding on one.
    Maҝe sure they have the safetʏ seal of approval.

    Method you can begin to play with self confidence. You wilⅼ want to gaіn access
    to yoᥙr winnings at year ’round as well as be
    able to find ɑ Casino Online betting game you feel ϲomfortable playing.
    Garments sites are very known to look at care ɑѕsociateɗ ԝith their customers and providing different levels of play.

    Of course, it iѕ often a usual thing to choose the best
    bonus for yoս but if you aren’t after gaining a lot, yߋu may choose a
    100% bonus with $50 lіmit for top stakes sports. With the ѕo many kinds money and item
    bonusеs online gamblers, you definitely have a lot of choices.
    It merely requires ɑ wrong choice elimіnate a lоt of moneу.
    It’s that easy.

    Remember, always try read through up important informatіon prior
    to bet, and figurе out the odds given by various webѕites
    for very same gɑme several leverage.

    Also visit my web ѕite: ifin.gov.so

發佈留言

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