Removing duplicates from Array - set

Hi there,

I read that you can remove duplicates from Arrays using Set. I am trying to do this but when i return unique[n] it returns undefined.

It seems like the error is here: unique = currentArr => […new Set(currentArr)];

Is there anything i am missing?

thanks,
yaz

function dblLinear(n) {

  //create a counter

  counter = 1;

  //create an array to store the sequence with 1 as starting

  let arr = [1];

  unique = currentArr => [...new Set(currentArr)];

  //for each index place, do the do two equations and add them to the end of the array

  const equation1 = current => 2 * current + 1;

  const equation2 = current => 3 * current + 1;

  for (let i = 0; i < n; i++) {

    let equation1res = equation1(arr[i]);

    let equation2res = equation2(arr[i]);

    arr.push(equation1res);

    arr.push(equation2res);

    arr.sort((a, b) => a - b);

    unique(arr);

  }

  //then sort each array into numbers

  //and then carry on the loop

  //once n = 10, return the value on that array index

  return unique[n];

}

console.log(dblLinear(20), 57);

No, your unique function is correct, except the fact it’s missing const.

You have some funky stuff going on here:

for (let i = 0; i < n; i++) {

    let equation1res = equation1(arr[i]);
    let equation2res = equation2(arr[i]);

  // Assuming that const arr = [1]; it has value only for i === 0, the rest is undefined

Hi I don’t quite mean what you mean.

function dblLinear(n) {
  //create a counter
  counter = 1;
  //create an array to store the sequence with 1 as starting
  let arr = [1];
  //const unique = currentArr => [...new Set(currentArr)];
  //for each index place, do the do two equations and add them to the end of the array
  const equation1 = current => 2 * current + 1;
  const equation2 = current => 3 * current + 1;

  for (let i = 0; i < n; i++) {
    let equation1res = equation1(arr[i]);
    let equation2res = equation2(arr[i]);
    arr.push(equation1res);
    arr.push(equation2res);
    arr.sort((a, b) => a - b);
    //unique(arr);
  }
  //then sort each array into numbers
  //and then carry on the loop
  //once n = 10, return the value on that array index
  //return unique[n];
  console.log(arr);
  return arr[n];
  
}

dblLinear(10);

gives back:
(21) [1, 3, 4, 7, 9, 10, 13, 15, 19, 21, 22, 27, 28, 31, 31, 39, 40, 43, 46, 58, 64]
1 22

I can console log the arr and return back the correct value. But, once I attempt to remove duplicate values, the unique arr is not recognised.

Did i misunderstand something?
thanks

think i got it :slight_smile:

function dblLinear(n) {
  //create a counter
  counter = 1;
  //create an array to store the sequence with 1 as starting
  let arr = [1];
  //for each index place, do the do two equations and add them to the end of the array
  const equation1 = current => 2 * current + 1;
  const equation2 = current => 3 * current + 1;

  for (let i = 0; i < n; i++) {
    let equation1res = equation1(arr[i]);
    let equation2res = equation2(arr[i]);
    arr.push(equation1res);
    arr.push(equation2res);
    arr.sort((a, b) => a - b);
    //unique(arr);
  }
  //then sort each array into numbers
  //and then carry on the loop
  //once n = 10, return the value on that array index
  //return unique[n];
  const unique = new Set(arr);
  unique;
  const back = [...unique];
  back;
  console.log(unique);
  console.log(back);
  return back[n];
}

dblLinear(10);