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,195,382 Responses

  1. Hey there I am so delighted I found your website, I really
    found you by accident, while I was looking on Aol for something else, Anyhow I am
    here now and would just like to say thanks a lot for a marvelous post and a all round thrilling blog (I also love the theme/design), I don’t have time to look over it all at the minute but I have saved
    it and also added in your RSS feeds, so when I have time I will be back to read
    more, Please do keep up the superb work.

  2. MichaelPayof表示:

    mexico drug stores pharmacies
    https://cmqpharma.online/# purple pharmacy mexico price list
    mexican drugstore online

  3. MichaelPayof表示:

    purple pharmacy mexico price list
    https://cmqpharma.online/# buying from online mexican pharmacy
    mexico drug stores pharmacies

  4. attorney表示:

    10 Facts About Medical Malpractice Lawyer That Will Instantly
    Put You In Good Mood attorney

  5. A motivating discussion is definitely worth comment.
    I believe that you ought to publish more on this subject, it may not be a taboo matter but usually people don’t speak about
    these topics. To the next! Many thanks!! https://Sportwest.COM.Ar/component/k2/item/5

  6. What You Need To Do On This Slots http://www.kaymell.uk

  7. MichaelPayof表示:

    mexico drug stores pharmacies
    https://cmqpharma.com/# buying prescription drugs in mexico online
    reputable mexican pharmacies online

  8. 10 Inspirational Graphics About Double Glazing Repairs Bedford Window Restoration bedford

  9. Hi i am kavin, its my first time to commenting anywhere,
    when i read this post i thought i could also make comment due to this sensible article.

    my blog post – genius wave free

  10. MichaelPayof表示:

    purple pharmacy mexico price list
    https://cmqpharma.com/# mexico pharmacies prescription drugs
    purple pharmacy mexico price list

  11. Ten Startups That Will Revolutionize The Birmingham Double Glazing
    Industry For The Better Local glaziers birmingham

  12. I am extremely impressed with your writing abilities as well as with the structure on your weblog.

    Is that this a paid subject or did you customize it yourself?
    Anyway stay up the nice high quality writing, it’s rare to see a great weblog like this one nowadays..

  13. elsycrays.top表示:

    15 Amazing Facts About Carlock Smith That You Never Knew elsycrays.top

  14. MichaelPayof表示:

    purple pharmacy mexico price list
    https://cmqpharma.online/# purple pharmacy mexico price list
    buying from online mexican pharmacy

  15. Just wish to say your article is as astounding. The clarity asbestos removal in ottawa your post is simply
    nice and i can assume you’re an expert on this subject. Fine with your permission let
    me to grab your RSS feed to keep up to date with forthcoming post.
    Thanks a million and please keep up the enjoyable work.

  16. 257634.xyz表示:

    This Is The Advanced Guide To Window Repairman 257634.xyz

  17. 11 Strategies To Refresh Your Asbestos Claims Law http://www.9363280.xyz

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

  19. The 10 Most Terrifying Things About Medical Malpractice Attorneys Medical malpractice attorneys

  20. 9 Things Your Parents Teach You About Window Repairs Bedford
    Window repairs bedford

  21. The 10 Scariest Things About Non Prescription ADHD Medication Uk non prescription adhd medication Uk

  22. Edda Fay表示:

    What Experts In The Field Want You To Know Edda Fay

  23. vffcu.biz表示:

    Link Building Tips Since It’s Afford To Miss 백링크 조회 (vffcu.biz)

  24. I am really pleased to rеad thiѕ web site posts which consists oof lots ⲟf valuable facts,
    thahks for providing thеse informatіon.

  25. gay porn表示:

    homosexual porn

發佈留言

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