Home > Blockchain >  randomize items in an array and keep original order
randomize items in an array and keep original order

Time:05-24

I would like to randomize the email addresses that are being output and remove duplicates and have them retain the original order. This works perfectly fine when I do not randomize. I generate the emails, remove dups, and output and have no issues. I also have no issues randomizing. The issue I seem to have is combining the two. Being able to generate the array, randomize, remove dups AND retain the original order. Below is what I have tried already, this is the closest I have gotten. Thanks for any help.

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(`[email protected]`);
sourceArray.push(`[email protected]`);
sourceArray.push(`[email protected]`);
sourceArray.push(`[email protected]`);
sourceArray.push(`[email protected]`);
sourceArray.push(`[email protected]`);
sourceArray.push(`[email protected]`);

// 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);

CodePudding user response:

If I understand correctly, to get your random array of emails I would do the following:

const arrayOfEmails = [];
for (let i = 0; i < 100000; i  ) {
  const randomInt = Math.floor(Math.random() * 100000); // random number between 0 and 999,999
  arrayOfEmails.push(`test${randomInt}@google.com`);
}

Then hopefully this helps as far as removing the dupes and keeping the order.

You could do

const array = [2,7,5,9,2,9,5,3,2,9]; // your random array
const set = new Set(array); // {2,7,5,9,3} javascript sets need unique members
const newArray = Array.from(set); // [2,7,5,9,3]

That's the easiest way I can think of.

If you didn't want to remove duplicates in a second step then you could also just write this:

const setOfEmails = new Set();
for (let i = 0; i < 100000; i  ) {
  const randomInt = Math.floor(Math.random() * 100000); // random number between 0 and 999,999
  setOfEmails.add(`test${randomInt}@google.com`); // will only add if the email is unique
}
const arrayOfEmails = Array.from(setOfEmails); // this array will be unique emails
  • Related