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,434,274 Responses

  1. Beau表示:

    %%

    Have a look at my homepage volkswagen keys replacement; Beau,

  2. Ӏt’ѕ awesome for mе to have a web site, wһich iѕ helpful for my knowledge.
    thanks admin

    Ꭲake a look at mmy web-site … beli followers ig termurah

  3. new表示:

    %%

    Also visit my blog post; new

  4. A fascinating discussion is definitely worth comment. There’s no doubt that that you ought to write more on this subject, it might not be a taboo
    subject but usually people do not discuss such issues.
    To the next! Kind regards!!

  5. lotto表示:

    Ꮋello, Neat post. Ƭhere is a problem tоgether with your website in internet explorer, may check tһis?
    IE nonethеless iѕ the market leader ɑnd a biց paгt
    of otһer people ᴡill pass over your fantastic writing ⅾue to
    this proЬlem.

    Alѕօ visit my site – lotto

  6. Nola表示:

    %%

    My blog cordless bluetooth headphones; Nola,

  7. crabs表示:

    It’ѕ genuinely very complicated in thus full of activity lifе to lіsten news onn TV, tһerefore I simplү
    use world wide web for that purpose, and get thee most
    up-to-date information.

  8. skins表示:

    %%

    Also visit my web site – skins

  9. sitemap表示:

    oof course like your wweb site but you have to
    take a look aat tthe selling on quite a ffew oof yourr posts.
    Seveal of them aare rife withh spelling issues and I find itt veey troublesomee tto nform thee trut
    then again I’ll certainly cme bacdk again.

  10. Karolin表示:

    door fitter reading – Karolin,

  11. cheap slut表示:

    Wonderful article! We are linking to this great article on our website.

    Keep up the good writing.

  12. Pretty nice post. I simply stumbled upon your blog and wanted to
    mention that I’ve truly loved browsing your blog posts. After all I will be subscribing in your feed and
    I hope you write once more soon!

  13. Cheap表示:

    %%

    Here is my blog post – Cheap

  14. I’m really impressed along with your writing abilities and also with the structure for your weblog.
    Is that this a paid theme or did you customize it your self?
    Anyway keep up the excellent quality writing, it’s uncommon to look a nice weblog like this one these days..

    My blog … calgarywasabi.com

  15. TimothyBrumb表示:

    sildenafil generic australia: where to buy sildenafil uk – sildenafil tablets 50mg

  16. There is certainly a great deal to know about this subject.
    I love all of the points you made.

  17. Saxenda表示:

    Simply desire to say your article is as surprising. The clarity in your post is simply great and i could assume you are an expert on this subject.

    Fine with your permission allow me to grab your RSS feed to keep updated with forthcoming post.
    Thanks a million and please keep up the rewarding work.

    my web blog Saxenda

  18. Lorrie表示:

    Thank you for another informative website.
    The place else may just I get that type of information written in such a perfect approach?

    I’ve a venture that I’m simply now operating on, and I have
    been on the look out for such info.

  19. %%

    My blog: case battle csgo (xunlianquan.com)

  20. I wanted to thank you for this very good read!! I certainly loved every bit of it.
    I have you bookmarked to check out new stuff you post…

  21. %%

    Also visit my blog post – jaguar Key fob programming (https://te.Legra.ph/)

發佈留言

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