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,627,280 Responses

  1. Williszew表示:

    http://indiamedfast.com/# IndiaMedFast

  2. May I simply say what a comfort to discover someone that genuinely knows what they are discussing
    on the net. You definitely know how to bring an issue to light and make it important.

    More and more people need to look at this and understand this side of the story.
    It’s surprising you aren’t more popular since you definitely have
    the gift.

  3. whoah this blog is excellent i like studying your articles.

    Stay up the great work! You realize, many individuals are searching round for this information, you could help them greatly.

  4. pdacenter.ru – сервис по ремонту бытовой техники
    Ремонт электросамокатов в Иркутске в официальном сервисном центре PDACENTER.
    Наши инженеры выполняют ремонт любой сложности по дотупным ценам!

  5. The one difference between professional and common pills is that Viagra Professional comes within the type
    of sublingual tablets. Is there a difference
    between the chemical formula of Viagra for males and Viagra for girls?
    There isn’t any such thing as homemade Viagra. The first thing to test if Viagra isn’t working is acceptable dosing.
    Sildenafil was first introduced to the market by Pfizer in 1998,
    as Viagra. 1993: Intel releases the primary Pentium processor.
    This condition is characterised by problem in attaining or maintaining an erection suitable for sexual activity.
    This could lead to blaming themselves and thinking
    that they are the issue when in actuality it is simply a bodily condition generally known as erectile
    dysfunction. You are not alone as round 1 million men in Australia
    are affected by a rectal dysfunction. Viagra is a medicine designed that will help
    you get and maintain an erection if you have erectile dysfunction (ED).
    This will increase blood move to your penis to get or maintain an erection. They
    are part of a category of medications called phosphodiesterase-5 (PDE5) inhibitors that
    work by rising blood movement to the penis. Why doesn’t Viagra work for everyone?

    If Viagra doesn’t work as it ought to or stops working, you’ll
    be able to take some steps to give the drug its
    finest probability to work.

  6. Thanks for the informative post. More at obtenez plus d’info

  7. RobertFab表示:

    Starda Casino is a modern online gaming portal offering an exciting world of gambling entertainment. Anyone looking for quality slots, card games and sports betting will find everything they need for entertainment and, perhaps, winning Starda Casino

  8. The realm of digital betting is continually advancing,
    with innovative platforms, functions, and possibilities appearing each year.

  9. JamesGag表示:

    cheapest online pharmacy india: India Med Fast – india pharmacy without prescription

  10. Williszew表示:

    http://interpharmonline.com/# canada rx pharmacy world

  11. Arthurhow表示:

    https://indiamedfast.com/# India Med Fast
    canadian pharmacy uk delivery

  12. unsplash.com表示:

    Very nice post. I just stumbled upon your blog and wanted to
    say that I’ve really enjoyed browsing your blog posts.
    After all I will be subscribing to your rss feed and I hope you
    write again soon!

  13. Montebeera表示:

    cheapest online pharmacy india online medicine shopping in india online medicine shopping in india

  14. Arthurhow表示:

    https://interpharmonline.com/# drugs from canada
    canada pharmacy online

  15. toket表示:

    Yes! Finally someone writes about Porn.

  16. JamesGag表示:

    mexican pharmacy online: mexican pharmacy online – Mexican Pharm Inter

  17. JamesGag表示:

    IndiaMedFast: india pharmacy without prescription – india pharmacy without prescription

  18. Williszew表示:

    https://mexicanpharminter.shop/# buying from online mexican pharmacy

  19. I all the time used to read article in news papers but now as I am a user of net thus from now I am using net for articles,
    thanks to web.

  20. Georgewhend表示:

    canadian 24 hour pharmacy: canada pharmacy no prescription – real canadian pharmacy

  21. It’s enormous that you are getting ideas from this paragraph
    as well as from our argument made here.

  22. Williszew表示:

    https://mexicanpharminter.com/# mexican pharmacy online

  23. Georgewhend表示:

    canadian valley pharmacy: Online pharmacy USA – canadian pharmacy no scripts

  24. Montebeera表示:

    reputable canadian online pharmacies highest rated canadian online pharmacy canadian drug prices

  25. Williszew表示:

    https://interpharmonline.shop/# vipps canadian pharmacy

發佈回覆給「Georgewhend」的留言 取消回覆

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