Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9118457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:09:31+00:00 2026-06-17T05:09:31+00:00

Possible Duplicate: Array value count javascript I have an array which contains several duplicates,

  • 0

Possible Duplicate:
Array value count javascript

I have an array which contains several duplicates, what I’m trying to achieve is to count how many duplicates each unique string has in this one array.

The array looks something like this

array = ['aa','bb','cc','aa','ss','aa','bb'];

Thus I would like to do something like this

if (xWordOccurrences >= 5) {
    // do something
}

But I’m not sure how I would code this.
I was thinking, create an object with each unique string, then loop through the original array, match each string with it’s object and increment it’s number by 1, then loop over the object to see which words had the most duplicates…

But this seems like an over complexe way to do it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T05:09:33+00:00Added an answer on June 17, 2026 at 5:09 am

    You can use an object which has keys of the Array’s values and do something like this

    // count everything
    function getCounts(arr) {
        var i = arr.length, // var to loop over
            obj = {}; // obj to store results
        while (i) obj[arr[--i]] = (obj[arr[i]] || 0) + 1; // count occurrences
        return obj;
    }
    
    // get specific from everything
    function getCount(word, arr) {
        return getCounts(arr)[word] || 0;
    }
    
    getCount('aa', ['aa','bb','cc','aa','ss','aa','bb']);
    // 3
    

    If you only ever want to get one, then it’d be more a bit more efficient to use a modified version of getCounts which looks similar to getCount, I’ll call it getCount2

    function getCount2(word, arr) {
        var i = arr.length, // var to loop over
            j = 0; // number of hits
        while (i) if (arr[--i] === word) ++j; // count occurance
        return j;
    }
    
    getCount2('aa', ['aa','bb','cc','aa','ss','aa','bb']);
    // 3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Copying array by value in javascript i have a funny problem with
Possible Duplicate: Getting a random value from a JavaScript array OK, so I have
Possible Duplicate: array.contains(obj) in JavaScript Let's say I have an array = [0,8,5] What
Possible Duplicate: How to count Matching values in Array of Javascript I have array
Possible Duplicate: php sort array by sub-value I have a multidimensional array like the
Possible Duplicate: array_count_values for javascript instead Let's say I have simple JavaScript array like
Possible Duplicate: check if value exists in array in Ruby I have this method
Possible Duplicate: For each in a array. How to do that in javascript? Hey
Possible Duplicate: Javascript - array.contains(obj) What's wrong with this: var zipCodes =(['90001','90002','90003']); Test if
Possible Duplicate: Counting occurences of Javascript array elements I have an array in javascript

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.