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,154,098 Responses

  1. SEO_miMa表示:

    продвижение сайтов в москве цена http://prodvizhenie-sajtov-v-moskve115.ru/ .

  2. SEO_toMa表示:

    продвижение сайтов в москве в яндекс и гугл https://prodvizhenie-sajtov-v-moskve115.ru/ .

  3. Janet表示:

    7 Tips About Misted Double Glazing Repairs Near Me That Nobody Can Tell You
    handle for double glazed window (Janet)

  4. bokep jilbab表示:

    Good write-up, I am normal visitor of one?s website, maintain up the excellent operate, and It’s going to be a regular visitor for a lengthy time.

  5. 5 Laws That Can Benefit The Motor Vehicle Lawyer Industry Motor Vehicle accident lawyers

  6. frli.dk表示:

    10 Wrong Answers To Common Vegas Slots Questions Do You Know The Right
    Answers? entertaining Slots (frli.dk)

  7. An asbestos lawsuit can be a way for the victim or their family members to receive compensation from
    the companies responsible for their exposure. Compensation can take the form of a jury verdict or settlement.

    My website – mesothelioma case

  8. Drug information sheet. Short-Term Effects.
    can you get cheap digoxin pill
    All what you want to know about medicines. Read here.

  9. I every time used to study paragraph in news papers but now as I
    am a user of net therefore from now I am using net for content, thanks to
    web.

  10. Evryone loves what yoou guys are ᥙр toο. Tһis type of clever ѡork and coverage!

    Қeep ᥙp thee superb wօrks guys Ι’ѵe incorporated yߋu guys to mу own blogroll.

    Αlso visit my web site :: Arena333 Slot Login Link Alternatif

  11. You’ll Never Guess This Window Replacement
    Near Me’s Tricks Window replacement near me

  12. Attorneys often prefer settlements because they are less risky, and clients can get compensation much faster.

    They also take into consideration the cost of treatment and wrongful death awards during settlement negotiations.

    Here is my web page – Asbestos Claim

  13. Medicines information for patients. Short-Term Effects.
    can i purchase generic sumatriptan online
    Everything trends of medicine. Get now.

  14. The 10 Most Terrifying Things About Online Grocery Stores That Ship online grocery stores that ship

  15. Compelling and thought-provoking investigation of the
    topic. Your examination was exhaustive and well-substantiated,
    furnishing participants with a exhaustive comprehension of
    the essential topics at grasp.

    I may be overjoyed to communicate complementary on this subject.
    If you agree, I would cordially request you to engage with us on the
    SBOBET community, where we can sustain our dialogue in a elevated dynamic domain.

    my website – online casino banking solutions (intensedebate.com)

  16. Hi there just wanted to give you a quick heads up. The text in your article seem to be running off the screen in Firefox.
    I’m not sure if this is a format issue or something to do with internet
    browser compatibility but I thought I’d post to let you know.
    The design look great though! Hope you get the issue fixed
    soon. Cheers

  17. 10 Things We All Do Not Like About Semi Truck Litigation semi truck Accident Lawyers

  18. Window Repair Near Me Tips To Relax Your Everyday Lifethe Only Window Repair Near Me Technique Every
    Person Needs To Learn Window Repair Near Me

  19. Lakesha表示:

    Where had been you?” he asked. Before she could answer, she said, he made a
    comment in regards to the very building she’d simply visited.

    My web blog Custom jewellery travel pouches (Lakesha)

  20. BomesJoype表示:

    Lorem Ipsum has been the industry’s standard dummy text https://innocap-goettingen.de/kzqrgo of type and scrambled it to make a type specimen book.

  21. slot features表示:

    11 Ways To Completely Sabotage Your Player Favorite Slots slot features

  22. Ну, правда надо знать как и что делать) неумехам в этом плане вряд-ли что-то светит.

  23. The Reason You Shouldn’t Think About How To Improve Your Leather
    Couch real leather couch

  24. See What Glazing Repairs London Tricks The Celebs Are Making Use Of glazing repairs london (tujuan.Grogol.us)

  25. 9 . What Your Parents Taught You About Upvc Windows Near Me
    upvc windows near me

  26. Pills prescribing information. Effects of Drug Abuse.
    buying cheap reglan prices
    Everything news about pills. Get now.

  1. 2024-07-01

    generic provigil united states

    generic provigil united states

發佈留言

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