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,520,114 Responses

  1. WOW just what I was searching for. Came here by searching for شیشه بالکن ریلی

  2. LeroyBak表示:

    mexican pharmaceuticals online mexico pharmacy mexican rx online

  3. Jorgeagopy表示:

    mail order pharmacy india: Pharmacies in India that ship to USA – indian pharmacy online

  4. 15 Gifts For The Double Glazing Repairs Eastleigh Lover In Your Life double glazing Eastleigh

  5. 10 No-Fuss Methods To Figuring Out Your Diagnosis Of ADHD how To Get a diagnosis for add

  6. Lightweight Motorized Folding Wheelchair: Myths And Facts Behind Lightweight Motorized Folding Wheelchair lightweight electric folding wheelchairs

  7. Great write-up! Media releases are essential for crafting media
    narratives annd sharing key details. They aid develop connections between companies and reporters.
    Developing effective press releases requires being direct, tailored
    with the concerns of targeted news channels. In the modern media landscape, press releazes
    also actt a important role in online PR strategies.
    They merely target traditional news outlets but also increase engagement and enhance a organization’s interneet visibility.

    Adding visuals, such aas clips, can render Illinois Press [damoa2019.maru.net] releases significantly captivating and accessible.
    Evolving to the developing media environment while preserving core values can greatly boost a press release’s impact.

    What’s your take on using multimedia in press releases?

  8. Хотите всегда быть в курсе праздничных дат? Наш сайт предлагает актуальную информацию о праздниках на каждый день в России и мире. Удобный календарь поможет узнать о предстоящих торжествах и не забыть поздравить близких с важными датами.
    этот день

  9. LeroyBak表示:

    herbal ed treatment cheap medication natural pills for ed

  10. nuddies表示:

    Hello there! Do you know if they make any
    plugins to help with SEO? I’m trying to get my blog to rank for some targeted keywords but I’m not seeing
    very good gains. If you know of any please share. Kudos!

  11. LeroyBak表示:

    indian pharmacy Pharmacies in India that ship to USA indian pharmacies safe

  12. Te.Legra.Ph表示:

    20 Trailblazers Leading The Way In Mercedes Spare Key Replacing
    Mercedes Key (Te.Legra.Ph)

  13. LeroyBak表示:

    reputable indian online pharmacy Pharmacies in India that ship to USA india pharmacy mail order

  14. Alicia表示:

    The History Of Sofas L Shape l shaped outdoor sofas (Alicia)

  15. Using Sally’s tutorial for her right facing poodle found here on The Objects of Design or my suggestions beneath, let’s start arranging the items.

  16. Thanks for every other informative web site.
    Where else may just I get that type of info written in such an ideal method?

    I’ve a challenge that I am simply now operating on, and I’ve been at the look out for such info.

  17. porn videos表示:

    What’s up to every body, it’s my first go to see of this webpage; this website carries amazing and truly fine data in support
    of visitors.

  18. memek表示:

    Hi there, I discovered your web site by way of Google while looking
    for a comparable matter, your web site got here up, it looks good.
    I’ve bookmarked it in my google bookmarks.

    Hello there, just turned into alert to your blog through Google, and found that
    it’s really informative. I am gonna be careful for brussels.
    I will appreciate should you continue this in future. A lot of other folks can be benefited from your writing.
    Cheers!

  19. Guide To ADHD Symptoms In Adult Women: The Intermediate Guide In ADHD Symptoms In Adult Women adhd symptoms in adult women

  20. LeroyBak表示:

    online shopping pharmacy india Pharmacies in India that ship to USA indian pharmacy paypal

  21. DevonPhack表示:

    http://pharm24.pro/# buy prescription drugs without doctor
    prescription drugs without doctor approval

  22. LeroyBak表示:

    buy prescription drugs from india Indian pharmacy to USA Online medicine order

發佈留言

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