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,647 Responses

  1. SamuelBix表示:

    where to buy cipro online: ci pharm delivery – buy generic ciprofloxacin

  2. KennethPah表示:

    prednisone medicine PredniBest prednisone tablets india

  3. RobertJar表示:

    cost of clomid prices: clomid on pharm – where to buy generic clomid now

  4. Just finished my case with a great auto accident lawyer ! They were professional and really knew their stuff

  5. Todd Bryant表示:

    The idea of personalizing user experience through web design is so relevant today! For more, visit rockland county webdesigner

  6. Viagra uses technique No. Three — it eliminates the PDE that’s decomposing the cGMP, so cGMP builds up within the penis and has
    a larger effect on the artery walls. Viagra/sildenafil is a
    ‘PDE5 inhibitor‘, but also impacts different PDE isozymes, resembling
    PDE6 (an enzyme within the retina) which can result in visible disturbance.
    They are often harmful when you’ve got sure underlying health conditions.
    Dr. Maree Corkeron researched how drug interactions can have an effect on well being and reported
    the findings within the Medical Journal of Australia 1.
    The warnings for Valium and Viagra are very related.
    They will overview your info and see whether one other therapy may match higher.

    Some Medicare Part C and Part D plans might cover the price of some versions
    of generic ED medications. It’s doubtless protected to take these medications together.
    Then, it’s up to you to assess if they are strong enough to detract from the
    advantages of taking Viagra Connect. Search and replace over
    your complete site (the result are a gazillion modifications which could be reverted like peculiar changes).

  7. Nikefut表示:

    darknet drug store dark web search engines tor darknet

  8. 7 Things You’ve Never Known About Mesothelioma Not Caused By Asbestos mesothelioma lawsuit

  9. Hello, i think that i saw you visited my site thus i came
    to “return the favor”.I’m trying to find things to improve my web site!I suppose its ok to
    use a few of your ideas!!

  10. Adeline Wells表示:

    This guide will definitely assist many homeowners prevent pricey errors regarding their roofing systems !! # anykeyword # roof flashing

  11. Angkanet表示:

    May I simply just say what a comfort to find
    an individual who genuinely understands what they’re discussing online.
    You definitely realize how to bring a problem to light and make it important.
    More and more people ought to read this and understand this side of the story.
    I was surprised that you’re not more popular because you most certainly have
    the gift.

  12. RobertJar表示:

    can i purchase generic clomid pill: how to get cheap clomid tablets – can i get generic clomid without a prescription

  13. EdgarWat表示:

    Интересные статьи о займах – https://www.apsny.ge/2021/other/1640192443.php

  14. arkada表示:

    Игровые автоматы Аркада приносят не только
    удовольствие, но и выигрыши!

    arkada

  15. SamuelBix表示:

    prednisone brand name india: order prednisone with mastercard debit – purchase prednisone from india

  16. SamuelBix表示:

    where can i buy cipro online: buy generic ciprofloxacin – where to buy cipro online

  17. Kristine表示:

    What Is SEO Software Tips To Relax Your Daily
    Life What Is SEO Software Trick That Every Person Must Know seo software; Kristine,

  18. SamuelBix表示:

    where to get generic clomid: generic clomid without prescription – can you buy clomid without prescription

  19. https://www.wpsue.com WPS Office: 一站式办公服务平台: 新升级,无广告,AI办公更高效. 立即下载. 登录使用. WPS 365: 面向组织和企业的WPS 365: 一站式AI办公,生产力即刻起飞. 了解更多. 咨询,记忆体占用低,体积轻运行快. 将文字、表格、演示、PDF等融合为一个组件。

  20. you’re really a excellent webmaster. The website loading pace
    is incredible. It sort of feels that you are doing any distinctive trick.
    Furthermore, The contents are masterpiece. you have performed a excellent process in this subject!

  21. With havin so much content and articles do you ever run into any
    issues of plagorism or copyright infringement?

    My website has a lot of exclusive content I’ve either created myself or
    outsourced but it looks like a lot of it is popping
    it up all over the web without my agreement. Do you know any techniques to help protect against content
    from being stolen? I’d certainly appreciate it.

發佈留言

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