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,199,070 Responses

  1. The 9 Things Your Parents Taught You About Veterans Disability Lawyer Veterans Disability Lawyer

  2. 14 Businesses Doing An Amazing Job At Adhd Assessment Adults adhd assessment for adults what to expect

  3. The Top Reasons Why People Succeed In The ADHD Diagnostic Industry adhd private diagnosis cost (https://freemaple.today/bbs/board.php?Bo_table=free&wr_id=204418)

  4. Cash-Back Cc Rewards – Now Back With A Vengeance 버팀목 대출

  5. 5 Killer Quora Answers On Double Glazed Near Me Double Glazed Near Me

  6. Online Sex表示:

    Nicely put, Regards.

  7. Upvc Windows Near Me Tools To Make Your Everyday Lifethe Only Upvc Windows Near
    Me Trick That Should Be Used By Everyone Learn upvc windows near me [http://bitetheass.com/]

  8. Why You Should Focus On The Improvement Of
    Demo Slot Gatot Kaca demo slot gatotkaca pragmatic

  9. How Glazing Repair Became The Hottest Trend In 2023 https://www.jerealas.top

  10. Keep on working, great job!

  11. Viola表示:

    The Three Greatest Moments In Door Repair Near Me History window replacement near me (Viola)

  12. What’s The Current Job Market For Window Doctor Near Me Professionals Like?

    window doctor near Me

  13. The 3 Most Significant Disasters In Personal Injury Attorney History Personal Injury Attorneys

  14. lawyer表示:

    See What Veterans Disability Lawyer Tricks The Celebs Are Utilizing lawyer

  15. A Positive Rant Concerning Motorcycle Accident Lawsuit
    motorcycle Accident lawyer

  16. Drug information. Long-Term Effects.
    where to get cheap erythromycin for sale
    Some trends of meds. Read information here.

  17. 7 Tips About Zeus Slot Pragmatic That Nobody Can Tell You Demo Slot Cq9 Zeus

  18. 5 Killer Quora Answers To Veterans Disability Attorneys Veterans Disability Attorney

  19. How To Tell If You’re At The Right Level To Go
    After Zeus Slot Machines trik Slot zeus Pragmatic

  20. Thanks a lot! Plenty of facts.

  21. The 10 Most Scariest Things About Main Sweet Bonanza Demo Main Sweet Bonanza Demo (Active.Popsugar.Com)

  22. What’s The Job Market For Double Glazed Window Repairs Professionals?

    double glazed window Repairs

  23. A List Of Common Errors That People Make Using Nissan Key Fob
    Replacement Nissan Pulsar Key Replacement

  24. 9 . What Your Parents Teach You About Double Glazed
    Window Suppliers Near Me double glazed window suppliers near me

  25. Just wish to say your article is as astonishing.
    The clarity in your publish is just nice and i could think you are
    an expert in this subject. Well along with your permission allow me to take hold of
    your feed to stay up to date with impending post.

    Thank you 1,000,000 and please keep up the rewarding work.

  26. 20 Fun Informational Facts About Malpractice Compensation Rock Falls Malpractice Attorney

  27. The 9 Things Your Parents Taught You About Bentley Key
    Fob Programming Bentley Key fob

  28. Please let me know if you’re looking for a
    article writer for your weblog. You have some really good articles and I feel I would
    be a good asset. If you ever want to take some of the load off, I’d
    absolutely love to write some content for your blog in exchange for a link back to mine.

    Please send me an e-mail if interested. Kudos!

  29. Pills information. Brand names.
    where can i get generic minocycline without rx
    Everything information about medicament. Get now.

發佈留言

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