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,251,712 Responses

  1. Узнай все о двустороннее варикоцеле варикоцеле причины

  2. Medicine information for patients. What side effects?
    where can i get bactrim without insurance
    Actual trends of drugs. Read information now.

  3. BrandenSlomy表示:

    cheap priligy: max pharm – dapoxetine online

  4. This is the perfect webpage for anybody who would like to understand this topic.
    You realize a whole lot its almost hard to argue with
    you (not that I really would want to…HaHa). You certainly put
    a fresh spin on a topic that has been discussed for decades.
    Wonderful stuff, just excellent!

  5. BrandenSlomy表示:

    amoxicillin 500 mg brand name: com pharm – buy cheap amoxicillin

  6. Very descriptive article, I liked that bit.
    Will there be a part 2?

  7. KasToto表示:

    Ԝе are a grou оf volunteers and ߋpening a brand neew
    scheme іn our community. Υour website provided uѕ with valuable information too work on.
    Y᧐u haѵe done an impressive process and ourr еntire community
    ѡill be thankful tо you.

    Нere is my bllg … KasToto

  8. I’ve been surfing on-line more than 3 hours these days,
    but I by no means found any fascinating article like yours.
    It is pretty value sufficient for me. In my opinion, if all web owners and
    bloggers made just right content material as you did, the internet will probably be
    a lot more helpful than ever before.

  9. Dereketerm表示:

    how to buy generic clomid online: rex pharm – get clomid no prescription

  10. It’s a pity you don’t have a donate button! I’d most certainly donate to this fantastic blog!

    I suppose for now i’ll settle for bookmarking and adding your
    RSS feed to my Google account. I look forward to
    new updates and will talk about this site with my Facebook group.
    Talk soon!

  11. Dereketerm表示:

    can you buy prednisone over the counter in canada: cheap prednisone – prednisone uk buy

  12. BrandenSlomy表示:

    clomid for sale: cheap clomid – where can i get clomid without prescription

  13. Dereketerm表示:

    30mg prednisone: cheap generic prednisone – 80 mg prednisone daily

  14. Dereketerm表示:

    how to buy generic clomid: rexpharm – generic clomid

  15. Dereketerm表示:

    amoxil generic: amoxil – canadian pharmacy amoxicillin

  16. Currently it seems like Movable Type is the preferred blogging platform out
    there right now. (from what I’ve read) Is that what you’re using on your blog?

  17. Nikefut表示:

    darkmarket list darknet drug store dark web markets

  18. Или никакого обучения? Абсолютно.

    Look into my web page – https://robotech.com/forums/viewthread/2227291

  19. 10 Strategies To Build Your Accident Attorney
    Empire accident and injury

  20. Thanks for sharing such a nice thought, piece of writing
    is good, thats why i have read it fully

  21. Pills information sheet. What side effects?
    spironolactone rebound edema
    All information about drug. Get information here.

  22. I have read several just right stuff here. Certainly price bookmarking for revisiting.
    I surprise how much attempt you put to make such a excellent informative website.

  23. StevenNub表示:

    amoxicillin 500 mg tablet price: amoxil – generic amoxicillin cost

  24. вывод из запоя на дому ростов-на-дону http://zarabotokdoma.creartuforo.com/viewtopic.php?id=11471/ .

發佈留言

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