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,238,586 Responses

  1. Howdy I am so thrilled I found your blog, I really found you by accident, while I was
    browsing on Askjeeve for something else, Nonetheless I am here now and would just like to say many thanks for
    a tremendous post and a all round entertaining blog (I
    also love the theme/design), I don’t have time to go through it all at the minute but I have bookmarked it and also added in your RSS feeds, so when I have time I will
    be back to read a great deal more, Please do keep up the excellent work.

  2. slot online表示:

    I couldn’t refrain from commenting. Perfectly written!

  3. Why Sports Toto Near Me May Be More Risky Than You Thought 먹튀검증 커뮤니티

  4. What’s up i am kavin, its my first time to commenting anyplace, when i read this post i thought i could also make comment
    due to this good article.
    https://virdsam.money/lomba-hkg/

  5. Jorgeagopy表示:

    online pharmacy india: Order medicine from India to USA – reputable indian pharmacies

  6. BrandonSaisy表示:

    india pharmacy: Indian pharmacy online – buy medicines online in india

  7. Jorgeagopy表示:

    Online medicine home delivery: Indian pharmacy worldwide delivery – world pharmacy india

  8. BrandonSaisy表示:

    viagra without doctor prescription: cheap medication – medications for

  9. Timzed表示:

    Just synchronize wallets and exchanges which you use, and koinly imports your agreements and calculates your taxes for bits via https://www.reverbtimemag.com/blogs_on/strategies-for-effectively-implementing-trading-bots for customers.

  10. Jorgeagopy表示:

    pharmacy website india: medicines from India – india online pharmacy

  11. LeroyBak表示:

    prescription drugs canada buy online cheap prescription drugs diabetes and ed

  12. Pills information. What side effects can this medication cause?
    buying aricept tablets
    All trends of medicines. Read here.

  13. Lazrqwp表示:

    Полезные советы по покупке диплома о высшем образовании без риска

    msfo-soft.ru/msfo/forum/user/46418

  14. Undeniably believe that that you said. Your favourite justification appeared to be on the
    web the easiest thing to be aware of. I say to you, I certainly get
    annoyed while people think about issues that they plainly do not understand about.
    You managed to hit the nail upon the highest and outlined out the whole thing without having side-effects , people
    can take a signal. Will likely be again to get more.
    Thank you

  15. 15 Best Masturbation Toys For Men Bloggers You Should Follow best masturbation toys

  16. 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.

  17. I am in fact thankful to the owner of this web page
    who has shared this impressive piece of writing at at this place.

  18. LeroyBak表示:

    mexico pharmacies prescription drugs mexican pharm 24 mexican border pharmacies shipping to usa

  19. Marty表示:

    20 Resources That Will Make You More Efficient With Get Car Key Cut spare car key cutting
    near me [Marty]

  20. data 6 d hk表示:

    If some one wishes expert view concerning running a blog then i suggest him/her to go to see this
    blog, Keep up the nice job.
    https://paitohk6d.city/

  21. ラブドール 男“Blessed is the season which engages the whole world in a conspiracy of love.”- Hamilton Wright MabieYour love and friendship have been a constant gift this past year.

  22. LeroyBak表示:

    reputable indian pharmacies medicines from India pharmacy website india

  23. LeroyBak表示:

    best online pharmacies in mexico Mexican pharmacy ship US best online pharmacies in mexico

  24. Jorgeagopy表示:

    medication from mexico pharmacy: mexico pharmacy – buying from online mexican pharmacy

發佈留言

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