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

992,397 Responses

  1. See What Auto Accident Lawsuit Tricks The Celebs Are Making Use Of Auto Accident Lawsuit

  2. jarum77表示:

    Wonderful web site. Plenty of helpful info here. I am sending it to several pals ans additionally
    sharing in delicious. And certainly, thank you on your effort!

  3. If you ѡant tο oЬtain a grеat deal from this post tgen you have tо apply such techniques to yоur wоn weblog.

    Tɑke a look at my web-site … link slot gacor terpercaya

  4. What’s The Job Market For Titration ADHD Medications Professionals?

    Titration adhd

  5. Meds information for patients. Cautions.
    how to buy generic benicar without dr prescription
    Some trends of medication. Get information now.

  6. Suzanna表示:

    You’ll Be Unable To Guess L Shaped Bunk Bed’s Benefits l shaped bunk beds with stairs;
    Suzanna,

  7. 5 Clarifications Regarding Online Shop Designer Suits best
    Luxury online shopping sites uk (miiglesiasaludable.com)

  8. Hello friends, fastidious post and pleasant urging commented here, I am really enjoying by these.

  9. Інтер Маямі представи офіціално перед своє фенове двете Сі нові зірки-Ліонел меси і Серхіо Бускетс.

    my site … https://fscmariupol.com.ua/all/vidoma-grupa-mariupolya-u-pershij-lizi/

  10. The 10 Most Terrifying Things About Sectional Sofas Sleeper Sectional Sofas Sleeper

  11. Pills information for patients. Effects of Drug Abuse.
    ran lisinopril 10 mg
    Everything news about medicament. Get now.

  12. Buck表示:

    Five Killer Quora Answers On GSA SER Search Engine Ranker gsa ser (Buck)

  13. hkb77表示:

    Do you have a spam issue on this website; I also am a blogger, and I was wondering your situation;
    we have created some nice procedures and we are looking to swap solutions with others, why not shoot me an email
    if interested.

  14. TAMPUNG99表示:

    I was curious if you ever thought of changing the
    layout of your website? Its very well written; I love what youve got to say.
    But maybe you could a little more in the way of content
    so people could connect with it better. Youve got an awful lot of text for only having one or two pictures.
    Maybe you could space it out better?

  15. 10 Misconceptions Your Boss Holds Regarding Workers Compensation Legal
    Workers’ compensation lawyer

  16. Medicament information for patients. Drug Class.
    cost of generic lisinopril online
    Some information about medicines. Get information here.

  17. 10 Healthy Habits For A Healthy Mobility Scooter For Adults best mobility Scooters

  18. Guide To 18 Wheeler Accident Attorney: The Intermediate Guide For 18 Wheeler Accident Attorney 18 wheeler accident attorney (cgi.members.interq.or.jp)

  19. It’s going to be end of mine day, except before
    finish I am reading this enormous piece of writing to increase my knowledge.

    my web blog green arrow costumes

  20. How To Find The Perfect Auto Folding Mobility Scooter With Suspension On The Internet automatic folding mobility Scooter

  21. jerealas表示:

    The Best Advice You Can Ever Get About Double Glazing Repairs Near Me jerealas

  22. You’ll Never Be Able To Figure Out This Couch L Shaped Sofa’s Tricks
    Couch L Shaped Sofa (Clinica-Elit.Vrn.Ru)

  23. Elsy Crays表示:

    20 Resources That’ll Make You More Efficient With Cars Locksmith Elsy Crays

  24. Leroy表示:

    Think You’re Perfect For Best Coffee Machines? Take This Quiz
    brewed coffee machine (Leroy)

發佈留言

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