I am trying to find a way to randomize a list of email addresses, remove any duplicates, and still maintain the original order. I have successfully achieved each task individually but struggle when combining them all together. Below is my attempt which is the closest I have come to achieving this. Any assistance would be greatly appreciated.
function randomize(arr) {
var i, j, tmp;
for (i = arr.length - 1; i > 0; i--) {
j = Math.floor(Math.random() * (i + 1));
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
return arr;
}
const sourceArray = [];
var arr = sourceArray;
// we start with an empty source array
// const sourceArray = [];
// the number of emails / 2
const numberOfEmails = 100000;
// first pass we add 100,000 emails
for (let index = 0; index < numberOfEmails; index++) {
sourceArray.push(`test${index}@google.com`);
}
// second pass we create dupes for all of them
for (let index = 0; index < numberOfEmails; index++) {
sourceArray.push(`test${index}@google.com`);
}
// throw in some extra dupes for fun
sourceArray.push(`<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f084958384c0b0979f9f979c95de939f9d">[email protected]</a>`);
sourceArray.push(`<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ee9a8b9d9adeae89818189828bc08d8183">[email protected]</a>`);
sourceArray.push(`<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="99edfceaeda9d9fef6f6fef5fcb7faf6f4">[email protected]</a>`);
sourceArray.push(`<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="34405147400474535b5b5358511a575b59">[email protected]</a>`);
sourceArray.push(`<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2b5f4e585f1b6b4c44444c474e05484446">[email protected]</a>`);
sourceArray.push(`<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1e6a7b6d6a2e5e79717179727b307d7173">[email protected]</a>`);
sourceArray.push(`<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ec98899f98dcac8b83838b8089c28f8381">[email protected]</a>`);
// this serves as a map of all email addresses that we want to keep
const map = {};
// an exact time before we run the algorithm
const before = Date.now();
// checks if the email is in the hash map
const isInHashmap = (email: string) => {
return map[email];
};
// iterate through all emails, check if they are in the hashmap already, if they are we ignore them, if not we add them.
sourceArray.forEach((email) => {
if (!isInHashmap(email)) {
map[email] = true;
}
});
// we fetch all keys from the hashmap
const result = Object.keys(map);
arr = randomize(arr);
console.log(`Randomized here: ${sourceArray}`);
console.log(`The count after deduplicating: ${result.length}`);
// gets the time expired between starting and completing deduping
const time = Date.now() - before;
console.log(`The time taken: ${time}ms`);
console.log(result);