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,145,233 Responses

  1. TylerPhona表示:

    https://cialist.pro/# Tadalafil price

  2. DavidOwelo表示:

    http://kamagra.win/# buy kamagra online usa

  3. Why You Must Experience Defra Exempt Wood Burner At Least Once In Your Lifetime defra certified stoves (extension.unimagdalena.edu.co)

  4. Great site. Lots of helpful info here. I’m sending it to several pals ans also sharing
    in delicious. And naturally, thank you in your effort!

  5. ElmerWes表示:

    Buy Viagra online cheap Cheapest place to buy Viagra sildenafil 50 mg price

  6. TylerPhona表示:

    https://cenforce.pro/# order cenforce

  7. Services表示:

    This Story Behind Audi Lost Key Can Haunt You Forever!
    Services

  8. 20 Resources That Will Make You More Successful At beko Washing machine deals – https://Mozillabd.science – Washers

  9. Hey! Do you know if they make any plugins to safeguard against hackers?
    I’m kinda paranoid about losing everything
    I’ve worked hard on. Any tips?

  10. Beulah表示:

    Where Can You Get The Most Reliable Order
    Avon Online Information? order avon online, Beulah,

  11. 10 Quick Tips On Car Key Programmer car key cut and program

  12. ElmerWes表示:

    Cheap Levitra online buy Levitra over the counter buy Levitra over the counter

  13. Ten Online Clothes Shopping Websites Uk That Will Make Your Life Better Retro Dining Chair

  14. Vimeo表示:

    The Ultimate Guide To 18 Wheeler Wreck Lawyer Vimeo

  15. You’ll Never Be Able To Figure Out This Shopping Online Uk To
    Ireland’s Tricks Shopping Online Uk To Ireland

  16. TylerPhona表示:

    http://levitrav.store/# Levitra tablet price

  17. ElmerWes表示:

    buy Kamagra kamagra.win Kamagra Oral Jelly

  18. Why No One Cares About Bags Prada Prada bags For sale

  19. 10 Unexpected Top Table Freezer Tips compact Freezers

  20. JamesNow表示:

    kamagra: buy kamagra online – Kamagra Oral Jelly

  21. DavidOwelo表示:

    http://viagras.online/# Generic Viagra for sale

  22. How To Tell If You’re Ready To GSA SER Help Gsa ser software

  23. ElmerWes表示:

    Cialis 20mg price in USA Generic Cialis without a doctor prescription cialis for sale

  24. JamesNow表示:

    Levitra generic best price: Buy Vardenafil 20mg – Buy Vardenafil online

  25. I’ve been exploring for a little bit for any high-quality articles or blog posts on this kind of area .
    Exploring in Yahoo I at last stumbled upon this site.
    Reading this info So i am glad to show that I’ve a very good uncanny feeling I discovered exactly what I needed.
    I so much surely will make sure to do not omit this web site and provides
    it a look on a constant basis.

    My blog post – solar hot water

  26. Five Killer Quora Answers To Motorcycle Accident
    Legal Ada motorcycle accident lawyer

  27. 10 Factors To Know On Birth Defect Litigation You Didn’t Learn At School
    birth Defect lawsuit

  28. 9 Lessons Your Parents Taught You About France Online Shopping Sites Clothes france online shopping sites clothes

發佈留言

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