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,586,106 Responses

  1. 高級 オナホdemonstrating the contagious nature of chronic anxiety within a family system.Chronic anxiety in a family can manifest in various ways,

  2. This Is The Complete Guide To Window Doctor Near Me Penthouse Window Repair

  3. 人形表示:

    人形 エロwas shot right here.TPE Dolls This sequence are all created by TPE content,

  4. Fridge Freezer Samsung Techniques To Simplify Your Everyday Lifethe Only
    Fridge Freezer Samsung Trick That Everyone Should Be Able
    To Freezer samsung

  5. Everyone’s life involves interactions with a wide spectrum of people,リアル ドールfrom our favorite,

  6.  Love and Will (1969),”is any natural function which has the power to take over the whole person.ラブドール エロ

  7. 14 Cartoons About Double Glazed Doors Near Me That’ll Brighten Your Day Replace A Double Glazed Window

  8. “What do I want?” and then,”What might be a better way to get what I want than getting mad?”Beware,ダッチワイフ

  9. upvc表示:

    14 Questions You Might Be Afraid To Ask About Windows Repairs Near Me upvc

  10. 人形表示:

    ラブドール オナホparticularly on narrowing the orgasm gap and female anatomy.Both men and women are eager to find ways to enhance female sexual pleasure,

  11. Why We Enjoy Door Repair Near Me (And You Should Also!) window replacement near me [http://www.google.co.Ls]

  12. How To Explain Door Fitters Milton Keynes To Your Grandparents window installation services milton keynes

  13. Male Adult Toys Tools To Streamline Your Everyday Lifethe
    Only Male Adult Toys Trick That Everyone Should Be Able To Male Adult Toys

  14. 5 Lessons You Can Learn From Shopping Online 627 Roll File adir

  15. researchers propose that these wiring differences result in men and women having different strengths.So,オナドール

  16. 10 Top Mobile Apps For Best Robot Vacuums best Self vacuum and mop

  17. 9 Things Your Parents Teach You About Link Alternatif Gotogel link Alternatif gotogel

  18. Allenacurf表示:

    pharmacie en ligne avec ordonnance: pharmacie en ligne france livraison internationale – pharmacie en ligne

  19. I’m not that mucfһ off a online reader to be honest but your sites really nice,
    keep it up! I’lⅼ go ahead and boօkmarkk
    your wеbsite too come back down the road.
    Cheers

    Visit my website wetogel (http://www.webwiki.co.uk)

  20. especially for first-time mothers.オナホ ラブドール Another study found that the transition to parenthood isn’t as hard on sexual satisfaction,

  21. dragontiger表示:

    These two, if such can complement strong advantages each other , ready to make a very
    favorable married couple.dragon tiger

    Stop by my blog post; dragontiger

  22. Hi, There’s no doubt that your website could possibly be having internet
    browser compatibility issues. Whenever I look at your blog in Safari, it looks fine however, if opening in I.E., it’s got some overlapping issues.
    I just wanted to provide you with a quick heads up! Apart from that, great
    website! https://Kolejowelk207.pl:443/index.php/Dyskusja_u%C5%BCytkownika:GeorgettaDescote

  23. 高級 ラブドールFinally,the researchers chose posts with keywords like “power” and “sex” to mirror past research on incels.

  24. manilabet365表示:

    After exploring a handful of the blog posts on your website,
    I honestly appreciate your way of writing a blog.
    I saved it to my bookmark site list and will be checking back soon. Take a look at
    my website too and let me know how you feel.

  25. 人形表示:

    ダッチワイフDoug was tall with dark hair and eyes,but it wasn’t his looks that unglued me.

  26. Thanks fߋr one’s marνelouѕ рostіng! I genuinely enjoyed reading it, you may be a
    ɡreаat author.І will remember tߋ bookmark your blog and will comne
    back sometime soon. I want to encourage yoᥙ to contjnue your grеat work, hae a nice holіday weekend!

    mү web-site … madrid 778 slot login

  27. How Boat Accident Claim Arose To Be The Top Trend In Social Media Boat Accident lawyer (classicalmusicmp3freedownload.com)

  28. ラブドール おすすめ friendship,and other kinds of meaningful relationships,

發佈留言

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