loading...

re: Find Duplicates in an Array VIEW POST

TOP OF THREAD FULL DISCUSSION
re: Qudos for posting your code so that others can see what you did. And critique it :-). I would have liked a copy of the workbench.js used in the or...
 

Hey, thanks for taking the time to review and comment the code!

I found and fixed my silly errors (I def. did not have enough coffee that morning, they were so obvious) and updated the stats in my original comment.

The slowest now is findObjectReduce, and the fastest one is findMapForCached.

Here is the new chart: live.amcharts.com/2MjY2/

And here is the updated code:

function findObjectFor(sampleData) {
  const seen = {};
  const dupes = [];

  for (let i = 0; i < sampleData.length; i++) {
    const item = sampleData[i];

    if (!seen.hasOwnProperty(item)) {
      seen[item] = item;
    } else {
      dupes.push(item);
    }
  }

  return dupes;
}

function findObjectForCached(sampleData) {
  const seen = {};
  const dupes = [];
  const length = sampleData.length;
  let i = 0;

  for (; i < length; i++) {
    const item = sampleData[i];
    if (!seen.hasOwnProperty(item)) {
      seen[item] = item;
    } else {
      dupes.push(item);
    }
  }

  return dupes;
}

function findObjectReduce(sampleData) {
  const seen = {};

  return sampleData.reduce((dupes, item) => {
    if (!seen.hasOwnProperty(item)) {
      seen[item] = item;
    } else {
      dupes.push(item);
    }

    return dupes;
  }, []);
}

function findMapFor(sampleData) {
  const seen = new Map();
  const dupes = [];

  for (let i = 0; i < sampleData.length; i++) {
    const item = sampleData[i];
    if (!seen.has(item)) {
      seen.set(item, item);
    } else {
      dupes.push(item);
    }
  }

  return dupes;
}

function findMapForCached(sampleData) {
  const seen = new Map();
  const dupes = [];
  const length = sampleData.length;

  let i = 0;

  for (; i < length; i++) {
    const item = sampleData[i];
    if (!seen.has(item)) {
      seen.set(item, item);
    } else {
      dupes.push(item);
    }
  }

  return dupes;
}

function findMapForReduce(sampleData) {
  const seen = new Map();

  return sampleData.reduce((dupes, item) => {
    if (!seen.has(item)) {
      seen.set(item, item);
    } else {
      dupes.push(item);
    }

    return dupes;
  }, []);
}

for (let r = 0; r < 1000; r++) {
  console.log(`run: ${r}`);

  let sampleData = [];
  // 50k array of random numbers
  for (let i = 0; i < 50000; i++) {
    sampleData[i] = Math.floor(Math.random() * 50000 + 1);
  }

  console.time("findObjectFor");
  findObjectFor(sampleData);
  console.timeEnd("findObjectFor");

  console.time("findObjectForCached");
  findObjectForCached(sampleData);
  console.timeEnd("findObjectForCached");

  console.time("findObjectReduce");
  findObjectReduce(sampleData);
  console.timeEnd("findObjectReduce");

  console.time("findMapFor");
  findMapFor(sampleData);
  console.timeEnd("findMapFor");

  console.time("findMapForCached");
  findMapForCached(sampleData);
  console.timeEnd("findMapForCached");

  console.time("findMapForReduce");
  findMapForReduce(sampleData);
  console.timeEnd("findMapForReduce");

  console.log();
}
Code of Conduct Report abuse