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,623,687 Responses

  1. It is perfect time to make a few plans for the longer
    term and it’s time to be happy. I have learn this post
    and if I may just I want to counsel you some attention-grabbing issues or tips.

    Perhaps you can write subsequent articles relating to
    this article. I wish to read even more things about it!

  2. Lazruuz表示:

    Приобретение диплома ПТУ с сокращенной программой обучения в Москве

    forum.7x.ru/member.php?u=16498

  3. DevonPhack表示:

    http://mexicanpharm24.cheap/# buying from online mexican pharmacy
    ed medicine

  4. slot gacor表示:

    What’s up i am kavin, its my first time to commenting anywhere, when i read this paragraph i thought i could also make comment due
    to this brilliant piece of writing.

  5. Lazrlls表示:

    Как правильно приобрести диплом колледжа или ПТУ в России, важные моменты
    kinooco.ru/?post_type=topic&p=23261

  6. click here表示:

    Hey there! I know this is kind of off topic
    but I was wondering which blog platform are you using for this website?
    I’m getting tired of WordPress because I’ve had issues with hackers and
    I’m looking at alternatives for another platform.
    I would be fantastic if you could point me in the
    direction of a good platform.

  7. Wow, wonderful blog layout! How lengthy have you been running a blog for?

    you make blogging look easy. The whole look of your web site is fantastic,
    as smartly as the content!

  8. BrandonSaisy表示:

    best male enhancement: low cost prescription – pain meds without written prescription

  9. bokep xnxx表示:

    This post is truly a nice one it assists new internet viewers, who are
    wishing in favor of blogging.

  10. LeroyBak表示:

    mexico pharmacies prescription drugs mexican pharmacy п»їbest mexican online pharmacies

  11. 5 Laws Everyone Working In Saab 93 Key Programming Should Be Aware Of saab key (https://yogaasanas.science)

  12. Lazrjgr表示:

    Приобретение диплома ПТУ с сокращенной программой обучения в Москве
    paintball-keller-lev.de/viewtopic.php?f=3&t=100302

  13. What’s The Current Job Market For Sofas Sectionals Professionals Like?
    Sofas Sectionals

  14. 10 Sites To Help You To Become An Expert In Mid Sleeper Small Single Metal Mid Sleeper Cabin Bed

  15. DanielVek表示:

    ЗАБАВНО)))
    astra is one among самых|наиболеепопулярных fast and flexible free https://medium.com/@wwwebadvisor/best-digital-agency-wordpress-themes-mostly-free-a4f64e0bd03f, known for its excellent responsiveness.

  16. LeroyBak表示:

    indian pharmacy online Pharmacies in India that ship to USA п»їlegitimate online pharmacies india

  17. I love your blog.. very nice colors & theme. Did you design this website yourself or did you hire
    someone to do it for you? Plz respond as I’m looking to construct my own blog and would like to find out where u got this from.
    cheers

  18. DevonPhack表示:

    http://indianpharm24.pro/# mail order pharmacy india
    ed medications list

  19. Lazrkbv表示:

    Полезные советы по безопасной покупке диплома о высшем образовании
    akademe.mn.co/posts/69577149

  20. alex star sex表示:

    Everyone loves what you guys are up too. This type of clever work and reporting!
    Keep up the very good works guys I’ve you guys to blogroll.

  21. Five Killer Quora Answers To Fridge Freezer French Doors Uk Fridge freezer French doors uk

  22. The Biggest Issue With Case Opening Battle, And How You Can Fix It Cs battle case

發佈回覆給「ยูฟ่าเบททางเข้าหลัก」的留言 取消回覆

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