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,243,318 Responses

  1. cammatch表示:

    It’s genuinely very complex in this active life to listen news on TV,
    so I only use web for that purpose, and obtain the newest information.

    Feel free to surf to my blog post – cammatch

  2. Lazrmrv表示:

    Легальная покупка диплома ПТУ с сокращенной программой обучения
    pansocialnetwork.mn.co/posts/69601958

  3. Nice post. I learn something new and challenging on blogs I stumbleupon on a
    daily basis. It will always be exciting to read through articles from
    other authors and practice something from other web sites.

  4. Nikefut表示:

    dark market darknet websites dark web link

  5. Узнай все о варикоцеле причины возникновения варикоцеле диагностика

  6. Lazrmyx表示:

    Приобретение диплома ПТУ с сокращенной программой обучения в Москве
    rrsclub.ru/showthread.php?p=50382#post50382

  7. Dereketerm表示:

    clomid generic: cheap clomid – order cheap clomid for sale

  8. Situs illegal表示:

    Jangan asal kasih data pribadi atau info finansial di situs
    yang nggak dikenal. Banyak banget scam di internet sekarang.
    Kalau nggak yakin, cari info dulu tentang situsnya!.

  9. BrandenSlomy表示:

    where buy cheap clomid now: cost of clomid pill – can i order clomid pills

  10. An outstanding share! I have just forwarded this onto a coworker who has been doing a little homework on this.
    And he actually bought me lunch simply because I found it for him…
    lol. So let me reword this…. Thank YOU for the meal!!
    But yeah, thanks for spending some time to talk about this issue here on your blog.

  11. What’s up i am kavin, its my first time to commenting anyplace,
    when i read this piece of writing i thought i could also create comment due to this sensible paragraph.

  12. With havin so much content do you ever run into any issues of plagorism or
    copyright violation? My site has a lot of unique content I’ve either created myself or outsourced
    but it seems a lot of it is popping it up all over the web without my authorization. Do you know any methods to help prevent content from
    being ripped off? I’d truly appreciate it.

  13. dương vật表示:

    This paragraph will help the internet viewers for setting up new webpage or even a blog from
    start to end.

  14. Medicines information leaflet. Cautions.
    can i cut bupropion hcl in half
    Everything information about pills. Read information here.

  15. BrandenSlomy表示:

    generic amoxicillin online: buy amoxil online – buy cheap amoxicillin online

  16. I have read so many articles concerning the blogger
    lovers however this article is actually a fastidious post,
    keep it up.

    Here is my site – เช่าชุดเจ้าสาว

  17. Узнай все о варикоцеле симптомы степени варикоцеле

  18. Hello! I’m at work surfing around your blog from my new iphone
    4! Just wanted to say I love reading your blog and look forward to all your posts!
    Carry on the excellent work!

  19. Dereketerm表示:

    dapoxetine price: max pharm – priligy

  20. You can certainly see your expertise in the work you write.
    The sector hopes for even more passionate writers like you who
    are not afraid to say how they believe. At all times go after your heart.

    My homepage; raise chickens

  21. 9 Lessons Your Parents Teach You About Double Buggy From Birth double buggy from birth

  22. Узнай все о варикоцеле и потенция двустороннее варикоцеле

  23. A motivating discussion is definitely worth comment.
    I do believe that you ought to write more on this subject
    matter, it may not be a taboo matter but typically
    folks don’t talk about these topics. To the next!
    Kind regards!!

  24. BrandenSlomy表示:

    prednisone 5 mg cheapest: ray pharm – prednisone 40mg

  25. Dereketerm表示:

    amoxicillin over the counter in canada: Amoxicillin for sale – where can i buy amoxicillin without prec

  26. See What Emergency Gas Engineer Tricks The Celebs Are Using Emergency Gas Engineer (Gas-Engineers-Near-Me24730.Tokka-Blog.Com)

  27. Mursorbit App表示:

    Hello there, just became aware of your blog through Google, and found
    that it’s really informative. I am going to watch out for
    brussels. I will appreciate if you continue this in future.
    Lots of people will be benefited from your writing.
    Cheers!

發佈留言

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