Y.E.S.
Y.E.S.

Reputation: 764

How to count value of multi dimensional arrays value

I have a large string array like:

var array = [["worker ", "department", "2", "6", "8", "3", "1", "12", ... , 14]
            ["1", "Duty/Not", "1", "0", "1", "0", "0", "1", ..., "0"]
            ["worker ", "department", "11", "10", "11", "3", ..., "7"]
            ["2", "Duty/Not", "0", "1", "1", "1", "1", "1", ..., "1"]];

In this array, every array of array row has got 15000 elements and there is a relation between first two subarrays and others. For example, the first row contains the first worker's department where s/he works. Otherwise, the second row contains the first worker's duty or not represent with 1 or 0.

I want to count worker's how many times be assigned to which department.

I tried to count using a loop. However, it takes a long time.

Edit:

A part of my implementation

   //Reading and parsing from csv file
    d3.text("worker.csv", function(text) {
       var data = d3.csvParseRows(text).map(function(row) {
           return row.map(function(string) {
       return string;
       });
   });


//Concatination 
   var concatArray = [].concat(...data);
//console.log(concatArray);

// Remove NULL from array var filteredAry = concatArray.filter(function(e) { return e != ""}); console.log(filteredAry);

//convert array row based
   var row = [], index = 0;
   for(var i = 0; i < element; i++){
      row[i]=filteredAry.filter(word => {index= 
          filteredAry.indexOf(word,index)+1;return ((index-1) % element) == i;});
       console.log(row[i]);
   }

Edit 2:

I convert to array column based. The output of array is like

["worker", "1"]
["depermant", "Duty/Not"]
["2", "1"]
["2", "1"]
["8", "1"]
["3", "0"]
["1", "0"]
["12", "1"]
["10", "1"]
["5", "0"]
["14", "1"]
["4", "1"]
["2", "1"]
["4", "1"]
["6", "1"]
["6", "0"]
["6", "0"]
 ["3", "1"]

Now, my question is almost same as before. I want to map department ids to the number of times they are being assigned, for every worker. For example, expected output is like

output = (1, 0),(2, 3), (3, 1), (4, 2), (5, 0), (6, 1)...

Upvotes: 1

Views: 302

Answers (3)

Markus Madeja
Markus Madeja

Reputation: 856

It's already solved but a solution for you original problem that should be somewhat fast would be:

// Testdata
var data = [["worker ", "department", "2", "6", "8", "3", "1", "12", 11],
            ["1", "Duty/Not", "1", "0", "1", "0", "0", "1", "0"],
            ["worker ", "department", "11", "10", "11", "3", "2", "3", 11],
            ["2", "Duty/Not", "0", "1", "1", "1", "1", "1", "1"]];
            
// We need to know the number of departments upfront for speedup            
const numOfDepartments = 12;
// First there are no worker in the departments
let countForEachDepartment = Array(numOfDepartments).fill(0);
let referrenceToDepartmentsInRow;

// go through each row
data.forEach((row, index) => {
  // always remove the first 2 columns because they are useless
  row.shift();
  row.shift();
  // are we in an odd or even row?
  if (index % 2) {
    // even row: we calc the numbers
    row.forEach((column, index) => {
      // the array start at one and we convert the string to a number with -0 to simply add it
      countForEachDepartment[referrenceToDepartmentsInRow[index] - 1] += (column-0);
    })
  } else {
    // odd row: we have the number to the departments that will work as a reference for even rows
    referrenceToDepartmentsInRow = row;
  }
});

console.log(countForEachDepartment);

Upvotes: 2

Y.E.S.
Y.E.S.

Reputation: 764

I solved the problem.

Solution:

var count = [], i, value, total = 0;
for (i = 0; i < array.length; i++) {
    value = array[i];
    if (typeof count[value] === "undefined") {
        count[value] = 1;

    } else {
        count[value]++;

    }
}

Upvotes: 1

Jay Lim
Jay Lim

Reputation: 412

I am not quite what you are trying to achieve here, but here are a few questions for you to think about:

  1. Do mind sharing your code so that we can get a better idea on what you are trying to achieve here?
  2. Where are you retrieving this data? Are you provided with this 2D array as it is or did you create it? If you are the one who generated the array, then one way to optimize it is to use a different data structure. If the array is provided as it is, then you will have to loop through all the elements at least once (i.e. 15000 entries/row * number of rows) to convert it into a different data structure.
  3. How many times are you trying to count? Is this a one time off or a repeated task? If it's a repeated task, I would recommend caching the final data structure or treat it as a global value so that you do not need to compute the count every time you need it.

Upvotes: 1

Related Questions