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,676,705 Responses

  1. You could definitely see your expertise within the work
    you write. The arena hopes for even more passionate writers like you
    who aren’t afraid to say how they believe. At all times go after your heart.

    Also visit my homepage :: sell my house for cash kansas city Missouri

  2. Data Hk表示:

    %%

    Feel free to visit my blog post … Data Hk

  3. MatthewSycle表示:

    https://withoutdoctorprescription.xyz/# tadalafil without a doctor’s prescription

  4. This site was… how do I say it? Relevant!! Finally I have found something that helped me.
    Cheers!

    My homepage – сборка кухни леруа мерлен цена

  5. Your mode of telling the whole thing in this piece of writing
    is in fact nice, every one can without difficulty understand it, Thanks a lot.

  6. Hi there, I enjoy reading all of your article post.
    I like to write a little comment to support you.

  7. Andreprark表示:

    https://paxlovid.best/# new covid medication

  8. Dennisscalp表示:

    https://paxlovid.best/# covid pill

  9. I think the admin of this web page is genuinely working hard
    in favor of his web site, as here every stuff is quality based data.

  10. musescore.com表示:

    great put up, very informative. I’m wondering why the other specialists of this
    sector don’t understand this. You should continue your writing.
    I’m sure, you have a great readers’ base already!

  11. Hey I know this is off topic but I was wondering if you knew of any widgets I could add to my blog
    that automatically tweet my newest twitter updates.
    I’ve been looking for a plug-in like this for quite
    some time and was hoping maybe you would have some experience with something like this.
    Please let me know if you run into anything. I truly enjoy reading your blog and I
    look forward to your new updates.

  12. click表示:

    В каком месте купить учетные записи валорант с скинами приложений реальной цене?

  13. Thank you for the good writeup. It in fact was a amusement account it.
    Look advanced to far added agreeable from you! By the way, how
    can we communicate? https://goegyx4.wixsite.com/my-site-19

  14. Hey! I just wanted to ask if you ever have any problems with hackers?
    My last blog (wordpress) was hacked and I ended up losing months of hard work due to no
    backup. Do you have any methods to prevent hackers?

    Also visit my webpage – เว็บความรู้

  15. environment表示:

    If some one wants to be updated with hottest technologies
    then he must be pay a quick visit this website and be up to date
    everyday.

  16. Slot online表示:

    I don’t even know how I stopped up here, but I thought
    this submit was great. I don’t realize who you are but
    definitely you are going to a well-known blogger
    if you happen to aren’t already. Cheers!

  17. personal ads表示:

    Great delivery. Sound arguments. Keep up the good effort.

    Also visit my site; personal ads

  18. sarang188表示:

    Do you mind if I quote a few of your articles as long as I provide
    credit and sources back to your weblog? My website is
    in the very same area of interest as yours and my users would genuinely benefit from some of the information you present here.
    Please let me know if this okay with you. Thanks a lot!

  19. Andreprark表示:

    https://molnupiravir.life/# molnupiravir philippines

  20. MatthewSycle表示:

    https://viagracanada.xyz/# viagra commercial actress 2015

  21. Christel表示:

    %%

    Here is my webpage: togel hkg (Christel)

  22. mostbet表示:

    To do this,

    My web site: mostbet

  23. Lamps表示:

    Truly no matter if someone doesn’t know afterward its
    up to other people that they will help, so here it occurs.

  24. Great items from you, man. I’ve keep in mind your stuff previous to and
    you are just too great. I really like what you have got right here,
    really like what you are saying and the best way by which you
    say it. You make it enjoyable and you continue to
    care for to stay it smart. I can’t wait to learn much more from you.

    That is actually a tremendous web site.

  25. Amazing! Its in fact amazing piece of writing, I
    have got much clear idea about from this post.

  26. Inredible points. Sound arguments. Keeep upp thhe amzzing effort.

    My blog; otto Kasino Cashback

  27. Peculiar article, totally what I was looking for.

    Also visit my web-site: crayola coloring pages

  28. Thank you for sharing indeed great looking !

  29. Hello i am kavin, its my first occasion to commenting anywhere, when i read this article i thought i could also make comment due to this
    good post. vape that rebranded

    Also visit my web page – drive cbd private label

發佈留言

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