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,229,959 Responses

  1. 20 Things That Only The Most Devoted Cheapest 12kg
    Washing Machine Fans Should Know Large Washing Machines 12Kg

  2. 9 Things Your Parents Teach You About Boat Accident Lawsuit boat Accident lawsuit

  3. 10 Ways To Build Your Can I Buy From A Uk Website Empire Red Kitchen Gadgets

  4. Marcelabelp表示:

    http://indiaph24.store/# online pharmacy india

  5. gocengqq表示:

    Whoa! This blog looks exactly like my old one! It’s on a completely different subject
    but it has pretty much the same page layout and design. Outstanding choice of colors!

  6. Ten 18 Wheeler Accident Law Firms That Really Improve Your Life 18 Wheeler Accident Law Firms

  7. Marcelabelp表示:

    http://indiaph24.store/# Online medicine home delivery

  8. 10 Wrong Answers To Common Mesothelioma Lawyer Questions: Do You Know The Correct Answers?

    asbestos Litigation

  9. Where Will Upvc Window Repairs Be 1 Year From In The Near Future?
    Upvc window repairs near me

  10. Rickysem表示:

    indian pharmacy paypal indian pharmacy mail order pharmacy india

  11. Violette表示:

    7 Small Changes You Can Make That’ll Make The Difference With Your Fold Away Treadmill affordable folding treadmill [Violette]

  12. И поскольку в рулетке действует теория вероятностей, в этой дисциплине применим математический и статистический анализ.

  13. 15 Gifts For The Door Fitters Leeds Lover In Your Life window hinge Repairs leeds

  14. Rickysem表示:

    mexican pharmaceuticals online mexico pharmacy mexican border pharmacies shipping to usa

  15. I’m gone to say to my little brother, that he should also pay a visit this weblog on regular basis to take updated from most recent news.

  16. What Is Mazda 2 Key Replacement And Why Is Everyone Speakin’ About It?
    Replacement mazda key (http://www.annunciogratis.net)

  17. fitted表示:

    Your Family Will Be Grateful For Having This Double Glazing Units
    Near Me fitted

  18. 15 Inspiring Facts About ADHD Diagnosis UK The
    Words You’ve Never Learned adult adhd diagnosis

  19. The Most Negative Advice We’ve Ever Heard About Beko Washing Machines New (https://Bookmarkspot.Win/)

  20. Pedroereva表示:

    purple pharmacy mexico price list: cheapest mexico drugs – best online pharmacies in mexico

  21. Guide To Treadmills Home: The Intermediate Guide To Treadmills Home Treadmills Home

  22. Marcelabelp表示:

    http://indiaph24.store/# indian pharmacy paypal

  23. It’s a shame you don’t have a donate button! I’d definitely donate to this excellent blog!
    I suppose for now i’ll settle for bookmarking and adding your RSS feed to my Google account.
    I look forward to fresh updates and will share this blog with my Facebook group.
    Talk soon!

  24. Stevenrip表示:

    best online pharmacy india: indian pharmacy – best india pharmacy

  25. porn video表示:

    I don’t even know how I ended up here, but I thought this post was great.
    I do not know who you are but certainly you are going to
    a famous blogger if you are not already 😉 Cheers!

  26. Terrific article! That is the type of info that are supposed to
    be shared across the net. Shame on the seek engines for now
    not positioning this submit upper! Come on over and consult with my web site .
    Thank you =)

  27. Blondell表示:

    The Three Greatest Moments In Foldable Electric Treadmill History folding electric treadmill – Blondell

  28. Hello! I’ve been reading your blog for some time now and finally
    got the courage to go ahead and give you a shout out from Atascocita Texas!
    Just wanted to tell you keep up the fantastic job!

  29. It’s actually a nice and helpful piece of info. I am glad that you just shared
    this useful info with us. Please stay us informed
    like this. Thank you for sharing.

發佈留言

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