# Removing duplicates from Array - set

**URL:** <https://forum.freecodecamp.org/t/removing-duplicates-from-array-set/335598>\
**Category:** General\
**Created:** [December 24, 2019, 9:52am UTC](https://forum.freecodecamp.org/t/removing-duplicates-from-array-set/335598 "2019-12-24T09:52:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![silvercut](https://avatars.discourse-cdn.com/v4/letter/s/94ad74/32.png) [@silvercut](https://forum.freecodecamp.org/u/silvercut)\
**Post date:** [December 24, 2019, 9:52am UTC](https://forum.freecodecamp.org/t/removing-duplicates-from-array-set/335598/1 "2019-12-24T09:52:55Z")

</div>

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

```auto
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);

```

---

<div class="post-metadata">

**Author:** ![snigo](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/snigo/32/128765_2.png) [@snigo](https://forum.freecodecamp.org/u/snigo)\
**Post date:** [December 24, 2019, 1:04pm UTC](https://forum.freecodecamp.org/t/removing-duplicates-from-array-set/335598/2 "2019-12-24T13:04:26Z")

</div>

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

You have some funky stuff going on here:

```auto
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

```

---

<div class="post-metadata">

**Author:** ![silvercut](https://avatars.discourse-cdn.com/v4/letter/s/94ad74/32.png) [@silvercut](https://forum.freecodecamp.org/u/silvercut)\
**Post date:** [December 26, 2019, 3:47am UTC](https://forum.freecodecamp.org/t/removing-duplicates-from-array-set/335598/3 "2019-12-26T03:47:20Z")

</div>

> **[Twice linear](https://www.codewars.com/kata/twice-linear/)**
>
> Codewars is where developers achieve code mastery through challenge. Train on kata in the dojo and reach your highest potential.

Hi I don’t quite mean what you mean.

```auto
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

---

<div class="post-metadata">

**Author:** ![silvercut](https://avatars.discourse-cdn.com/v4/letter/s/94ad74/32.png) [@silvercut](https://forum.freecodecamp.org/u/silvercut)\
**Post date:** [December 26, 2019, 4:05am UTC](https://forum.freecodecamp.org/t/removing-duplicates-from-array-set/335598/4 "2019-12-26T04:05:13Z")

</div>

think i got it 🙂

```auto
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);

```

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 16, 2021, 11:29am UTC](https://forum.freecodecamp.org/t/removing-duplicates-from-array-set/335598/5 "2021-01-16T11:29:12Z")

</div>

Closed on January 16, 2021, 11:29am UTC


