Skip to main content
Post Undeleted by dting
Post Deleted by dting
added 753 characters in body
Source Link
dting
  • 39.4k
  • 10
  • 98
  • 117

You can use an object to track how many unique items you have seen if your objects are hashable:

If you want to stop after you add a third unique value:

var items = [[1,1,2,2,3,3,4,4,5,5],[0,0,0,1,0,1,0,1,0,1]];

function solve(items) {
  var seenobj = Object.create(null);
  var iseen = 0;
  var i = -1;
  while (i++i < items[0].length && Object.keys(seen).length < 3) {
    seen[items[0][i]]var element = items[0][i];
    if (element in obj) continue;
    obj[element] = true;
    i++;seen++;
  }
  return [items[0].slice(0,i), items[1].slice(0,i)];
};

document.getElementById('result').innerHTML = JSON.stringify(solve(items));
<pre id="result"></pre>

If you want to break out of the loop when the 4th unique value is seen, you can do it like this:

var items = [[1,1,2,2,3,3,4,4,5,5],[0,0,0,1,0,1,0,1,0,1]];

function solve(items) {
  var seenobj = Object.create(null);
  var iseen = 0;
  var i = -1;
  while (i++i < items[0].length && Object.keys(seen).length < 4) {
    seen[items[0][i]]var element = items[0][i];
    if (element in obj) continue;
    obj[element] = true;
    i++;seen++;
  }
  return [items[0].slice(0,i-1), items[1].slice(0,i-1)];
};

document.getElementById('result').innerHTML = JSON.stringify(solve(items));
<pre id="result"></pre>

You can use an object to track how many unique items you have seen if your objects are hashable:

If you want to stop after you add a third unique value:

var items = [[1,1,2,2,3,3,4,4,5,5],[0,0,0,1,0,1,0,1,0,1]];

function solve(items) {
  var seen = Object.create(null);
  var i = 0;
  while (i < items[0].length && Object.keys(seen).length < 3) {
    seen[items[0][i]] = true;
    i++;
  }
  return [items[0].slice(0,i), items[1].slice(0,i)];
};

document.getElementById('result').innerHTML = JSON.stringify(solve(items));
<pre id="result"></pre>

If you want to break out of the loop when the 4th unique value is seen, you can do it like this:

var items = [[1,1,2,2,3,3,4,4,5,5],[0,0,0,1,0,1,0,1,0,1]];

function solve(items) {
  var seen = Object.create(null);
  var i = 0;
  while (i < items[0].length && Object.keys(seen).length < 4) {
    seen[items[0][i]] = true;
    i++;
  }
  return [items[0].slice(0,i-1), items[1].slice(0,i-1)];
};

document.getElementById('result').innerHTML = JSON.stringify(solve(items));
<pre id="result"></pre>

You can use an object to track how many unique items you have seen if your objects are hashable:

If you want to stop after you add a third unique value:

var items = [[1,1,2,2,3,3,4,4,5,5],[0,0,0,1,0,1,0,1,0,1]];

function solve(items) {
  var obj = Object.create(null);
  var seen = 0;
  var i = -1;
  while (++i < items[0].length && seen < 3) {
    var element = items[0][i];
    if (element in obj) continue;
    obj[element] = true;
    seen++;
  }
  return [items[0].slice(0,i), items[1].slice(0,i)];
};

document.getElementById('result').innerHTML = JSON.stringify(solve(items));
<pre id="result"></pre>

If you want to break out of the loop when the 4th unique value is seen, you can do it like this:

var items = [[1,1,2,2,3,3,4,4,5,5],[0,0,0,1,0,1,0,1,0,1]];

function solve(items) {
  var obj = Object.create(null);
  var seen = 0;
  var i = -1;
  while (++i < items[0].length && seen < 4) {
    var element = items[0][i];
    if (element in obj) continue;
    obj[element] = true;
    seen++;
  }
  return [items[0].slice(0,i-1), items[1].slice(0,i-1)];
};

document.getElementById('result').innerHTML = JSON.stringify(solve(items));
<pre id="result"></pre>

Post Undeleted by dting
added 753 characters in body
Source Link
dting
  • 39.4k
  • 10
  • 98
  • 117

You can use an object to track how many unique items you have seen if your objects are hashable:

If you want to stop after you add a third unique value:

var items = [[1,1,2,2,3,3,4,4,5,5],[0,0,0,1,0,1,0,1,0,1]];

function solve(items) {
  var seen = Object.create(null);
  var i = 0;
  while (i < items[0].length && Object.keys(seen).length !< 3) {
    seen[items[0][i]] = true;
    i++;
  }
  return [items[0].slice(0,i), items[1].slice(0,i)];
};

document.getElementById('result').innerHTML = JSON.stringify(solve(items));
<pre id="result"></pre>

If you want to break out of the loop when the 4th unique value is seen, you can do it like this:

var items = [[1,1,2,2,3,3,4,4,5,5],[0,0,0,1,0,1,0,1,0,1]];

function solve(items) {
  var seen = Object.create(null);
  var i = 0;
  while (i < items[0].length && Object.keys(seen).length < 4) {
    seen[items[0][i]] = true;
    i++;
  }
  return [items[0].slice(0,i-1), items[1].slice(0,i-1)];
};

document.getElementById('result').innerHTML = JSON.stringify(solve(items));
<pre id="result"></pre>

You can use an object to track how many unique items you have seen if your objects are hashable:

var items = [[1,1,2,2,3,3,4,4,5,5],[0,0,0,1,0,1,0,1,0,1]];

function solve(items) {
  var seen = Object.create(null);
  var i = 0;
  while (i < items[0].length && Object.keys(seen).length != 3) {
    seen[items[0][i]] = true;
    i++;
  }
  return [items[0].slice(0,i), items[1].slice(0,i)];
};

document.getElementById('result').innerHTML = JSON.stringify(solve(items));
<pre id="result"></pre>

You can use an object to track how many unique items you have seen if your objects are hashable:

If you want to stop after you add a third unique value:

var items = [[1,1,2,2,3,3,4,4,5,5],[0,0,0,1,0,1,0,1,0,1]];

function solve(items) {
  var seen = Object.create(null);
  var i = 0;
  while (i < items[0].length && Object.keys(seen).length < 3) {
    seen[items[0][i]] = true;
    i++;
  }
  return [items[0].slice(0,i), items[1].slice(0,i)];
};

document.getElementById('result').innerHTML = JSON.stringify(solve(items));
<pre id="result"></pre>

If you want to break out of the loop when the 4th unique value is seen, you can do it like this:

var items = [[1,1,2,2,3,3,4,4,5,5],[0,0,0,1,0,1,0,1,0,1]];

function solve(items) {
  var seen = Object.create(null);
  var i = 0;
  while (i < items[0].length && Object.keys(seen).length < 4) {
    seen[items[0][i]] = true;
    i++;
  }
  return [items[0].slice(0,i-1), items[1].slice(0,i-1)];
};

document.getElementById('result').innerHTML = JSON.stringify(solve(items));
<pre id="result"></pre>

Post Deleted by dting
Source Link
dting
  • 39.4k
  • 10
  • 98
  • 117

You can use an object to track how many unique items you have seen if your objects are hashable:

var items = [[1,1,2,2,3,3,4,4,5,5],[0,0,0,1,0,1,0,1,0,1]];

function solve(items) {
  var seen = Object.create(null);
  var i = 0;
  while (i < items[0].length && Object.keys(seen).length != 3) {
    seen[items[0][i]] = true;
    i++;
  }
  return [items[0].slice(0,i), items[1].slice(0,i)];
};

document.getElementById('result').innerHTML = JSON.stringify(solve(items));
<pre id="result"></pre>