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,246,671 Responses

  1. Nikefut表示:

    darknet seiten dark websites deep web links

  2. Hmm it looks like your site ate my first comment (it
    was super long) so I guess I’ll just sum it up what I
    had written and say, I’m thoroughly enjoying your blog. I too am an aspiring blog writer but I’m still new to the whole thing.
    Do you have any recommendations for rookie blog writers? I’d certainly appreciate it.

  3. Apple struck gold with the iMac. Now it wants to push its Windows opponents right out of the
    spectrum. Macworld tastes Apple’s new colored iMacs. Warning: the following info will not be for the colour blind, or for any stick-in-the-muds nonetheless in love with their previous beige
    computers. In case you haven’t been studying Macworld
    recently or your telly’s on the blink, you won’t know that Apple’s iMac – one of the best-promoting shopper Pc within the US and many other parts of the world
    (Hooray!) – is now obtainable in a fruity vary of new colours: blueberry,
    grape, lime, strawberry and tangerine. Apple struck gold with the iMac – its most interesting-ever client Pc.
    For the first time in years, it pushed Compaq and Dell into silver and bronze
    positions. And now it needs to push its Windows competitors proper out of
    the spectrum. And it’s not simply the colours which have changed.
    The new fruit-flavoured iMacs are quicker than the Bondi Blue original, have extra video reminiscence than the very first incarnation, and sport larger onerous
    drives. The iMac simply retains getting higher, however don’t
    grasp round waiting for spiffier features and cheaper prices – for those who need one,
    stop losing time and purchase one in the present day.

    On this assessment of today’s iMacs, we’ll inform you why these are the very best consumer Macs (certainly, best client laptop ever), undertake some indepth color research,
    and reply your most incessantly requested questions.

    The 5 new colours – or “flavours” as Apple dubs them – are a
    juicier bunch than the beige boxes out there everywhere else.
    Take a trip to Pc World or Dixons, and inform me which laptop stands out from the
    group.

  4. AlbertDuM表示:

    Clopidogrel 75 MG price: buy Plavix Clo – buy clopidogrel online

  5. BrandenSlomy表示:

    can i get clomid price: cheap clomid – where to buy clomid

  6. cuaca778表示:

    Hey I know this is off topic but I was wondering
    if you knew of any widgets I could add to my blog that automatically tweet my newest twitter updates.
    I’ve been looking for a plug-in like this for quite some time and was hoping maybe you would have some experience with something
    like this. Please let me know if you run into anything.
    I truly enjoy reading your blog and I look forward to your new updates.

  7. Seth Townsend表示:

    Appreciate the detailed insights. For more, visit Cosmetic Dentistry Baton Rouge

  8. If you are a fan of magic reveals, don’t pass over the alternative to peer a few thoughts-blowing performances in Las Vegas. From David Copperfield to Penn & Teller, the urban supplies a considerable number of magical stories strip clubs Las Vegas

  9. cat toys表示:

    Great post. I was checking continuously this blog and I’m inspired!
    Very useful information particularly the final phase 🙂 I
    take care of such info much. I used to be seeking this particular info for a long time.
    Thanks and good luck.

  10. Vincent Pope表示:

    The team at P&J Cleaners was punctual and very professional residential cleaning vancouver

  11. ดอกไม้เป็นสิ่งที่สร้างความสุขและความประทับใจได้เสมอ ด้วยร้านดอกไม้ใกล้ฉันที่หาได้ง่ายในตอนนี้
    คุณไม่ต้องเสียเวลาเดินทางไกล ดอกไม้ตกแต่งงานแต่งงาน
    ตอบโจทย์ทุกความต้องการอย่างลงตัว หากคุณกำลังมองหาดอกไม้สวยๆ ในราคาที่คุ้มค่า ลองดูร้านใกล้บ้านที่มีบริการจัดส่งด่วน คุณมีคำแนะนำสำหรับร้านดอกไม้ใกล้ฉันไหม?
    ร้านดอกไม้ใกล้ฉัน

  12. AlbertDuM表示:

    buy Clopidogrel over the counter: Clopidogrel Best Prices – antiplatelet drug

發佈留言

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