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,427,444 Responses

  1. BrandenSlomy表示:

    how to buy cheap clomid tablets: generic clomid – cost clomid without insurance

  2. web site表示:

    Appreciating the time and energy you put into your site and
    detailed information you offer. It’s great to come across a blog every once in a
    while that isn’t the same out of date rehashed material.

    Great read! I’ve bookmarked your site and I’m including your RSS feeds to my Google account.

  3. BrandenSlomy表示:

    how to buy amoxicillin online: com pharm – cost of amoxicillin 30 capsules

  4. 20112024AB表示:

    First of all I would like to say great blog! I had a quick question in which I’d like to ask if you
    don’t mind. I was interested to know how you center yourself and clear your mind prior to writing.
    I’ve had trouble clearing my mind in getting my thoughts out there.
    I truly do enjoy writing however it just seems
    like the first 10 to 15 minutes are generally lost simply just trying to figure out how to begin. Any ideas or tips?
    Thanks!

  5. This is very attention-grabbing, You are a very
    professional blogger. I have joined your rss feed and
    stay up for seeking extra of your fantastic post. Also, I
    have shared your site in my social networks

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

  7. Have you ever considered about adding a little bit more
    than just your articles? I mean, what you say is
    fundamental and everything. Nevertheless imagine
    if you added some great images or videos to give your posts
    more, “pop”! Your content is excellent but with pics and video clips, this website could certainly be one of the
    best in its field. Amazing blog!

  8. Dereketerm表示:

    can i order generic clomid online: buy clomid – cost generic clomid without dr prescription

  9. Производство павильонов с доставкой и установкой

  10. slot gacor表示:

    Nice blog here! Also your website loads up fast!
    What web host are you using? Can I get your affiliate link to your host?
    I wish my site loaded up as fast as yours lol

  11. LamarFob表示:

    Drug trends described. Patient medication guide.
    buy proscar online
    Short-term impacts described. Latest medication news.

  12. Dereketerm表示:

    dapoxetine price: priligy max pharm – buy priligy

  13. Dereketerm表示:

    cheap priligy: Priligy tablets – buy priligy

  14. Informative article, exactly what I wanted
    to find.

  15. Michael表示:

    Telegram 是一款主打隐私保护和高安全性的即时通讯应用,广泛支持全球用户。虽然 Telegram 没有发布专门的“中文版”,但其支持简体中文和繁体中文语言,用户可以通过设置轻松切换到中文界面。Telegram 提供了包括加密聊天、语音通话、文件传输、大型群组和频道等丰富的功能,并且支持跨平台同步,用户可以在手机、电脑等设备上无缝使用。https://www.telegramoo.com

  16. BrandenSlomy表示:

    get generic clomid tablets: generic clomid – where buy clomid price

  17. Dereketerm表示:

    max pharm: cheap priligy – priligy max pharm

  18. Hello there, You’ve done an excellent job. I’ll definitely digg it and personally suggest to my friends.
    I’m sure they will be benefited from this website.

  19. Dereketerm表示:

    prednisone cost 10mg: cheap prednisone – prednisone cost us

  20. I like reading an article that will make men and women think.
    Also, many thanks for permitting me to comment!

  21. Hello, i read your blog from time to time and i own a similar one and
    i was just curious if you get a lot of spam comments? If so how do you protect against it, any plugin or anything you can suggest?
    I get so much lately it’s driving me mad so any help is very much
    appreciated.

  22. Markdob表示:

    Приветствую. Может кто знает, где найтиразные блоги о недвижимости? Пока нашел – https://geotech48.ru

  23. StevenNub表示:

    dapoxetine online: max pharm – priligy

  24. Markdob表示:

    Приветствую. Может кто знает, где почитатьполезные блоги о недвижимости? Пока нашел – https://geotech48.ru

  25. You should take part in a contest for one of the greatest
    blogs on the web. I am going to highly recommend
    this website!

發佈留言

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