Remove dublicates from an array

Hello, dear experts! Today I have the challenge to create a JS function that receives an array of integers as an argument and removes all duplicates from the array and returns it. The problem is that I’m not allowed to use any built-in methods like IndexOf(), map() etc. Any ideas?

You could use the approach of the hash set and implement it with Set or Object.
Hash set is a collection of elements that has no duplicate, I am using js built in class of Set for simplified code. You could use Object to acheive the same thing, just treat the key as the unique element in your object.


  1. initiate a empty Set for recording what number we have seen
  2. start to loop thru the array
    a. for each loop, check the integer is available in the Set or not
    b. if the integer is not available in the set, save this new integer to Set, else do nothing
    c. continue to the next loop
  3. upon completion of the loop, all unique elements should appear once in the Set. Convert the Set to an array
  4. return the array


  • Time: O(n), we will loop thru an entire array of input once, with each loop the check unique iperation will be O(1) because it is one time look up when checking an element against hash set

  • Space: O(2n), the max memory we will be using is if all elements in the array are unique, we got the 2n from

    1. the hash set we use to validate and the entire length of the array, hence 1n
    2. the result array that contains all unique elements, hence 1n


const dedupe = (numbers) => {
   const haveSeen = new Set();

   for(let i = 0; i < numbers.length; i++) {
       const number = numbers[i];
       const isNew = !haveSeen.has(number);
       if(isNew) {

   const uniqueNumbers = Array.from(haveSeen);

   return uniqueNumbers;
1 Like

Thank you so much for this quick reply, but the forEach shouldn’t be included either…

@colebcol np, replaced foreach with good old for loop

If you’re using Set, what’s the point of checking if the set already has that number?

Hi there!

I call this challenge a “UNIQUE ARRAY SORTING MACHINE” .

Please allow me to suggest the following steps I had made when I went through this same challenge, and made it:

Note: The intention is to create a code that will sort (in ascending/descending order of choice) any given number of one-dimensional arrays as an argument (if only one array), or arguments (if a collection of arrays) to a function declaration.

//step 1: create an array ‘arr1’ container to consolidate all segregated elements from ‘args’.
function uniteUnique(…args) {
let arr1 = ;
console.log(args); //used to monitor ‘args’ contents

//step 2: create a ‘for loop’ to handle any length of ‘args’; then push each ‘unpacked’ array element from ‘args’ to ‘arr1’ container, while monitoring contents of ‘arr1’.

//step 3: Now that all unpacked array elements have been transferred to ‘arr1’ container, ‘arr1’ elements are then sorted (in ascending order) to create an order of arrangement (like, ‘house-keeping’).

Now, although some sense of order has been created using ‘array.sort()’ method, the real challenge at this point is how to create an array of ‘unique elements’ (non-duplicated, that is!) in the same ascending order (or, descending order is also possible).

//step 4: Create a container that will hold only the ‘unique elements’ from the initially sorted ‘arr1’ array ==>>
let uniqueArray = ;

//step 5: Create a ‘recursive’ function that will perform a ‘unique sorting’ of elements from ‘arr1’; this function creates flexibility in handling any lengths, ‘max’ and ‘min’ values of ‘arr1’ array being evaluated.

//At this point, ‘uniqueArray’ would have already collected and sorted (in order of choice) all the unique elements from the initially sorted array ‘arr1’.

//Happy coding!

myQuote: “javaScript is sweeter the second time around” @joMari

Or you just use Set (as mentioned already):

const unique = [ Set(myArr)]

Problem solved, unless Set isn’t allowed, too.

It would be a bit odd if Set was allowed considering the challenge seems to want it done “manually” using no array methods.

But we don’t know the challenge so that is just speculation. Also, the test might just not account for using Set as well.

I am not quite sure about “Set”, as I 'm relatively new to JS and this is a part of my assignment. So it goes, that I’m not allowed to use any external files or libraries. Together with built in methods.

You can always check MDN.

But the way you describe it I would be surprised if Set was allowed. That is just too easy considering the other requirements.

Probably true. Sounds like “solve this only with for loops and if/else statements”. Could be interesting to try.

Can you at least use .push? Or no array methods at all?

I mean you don’t really need push either.

But at that point you are almost just coding C. Which isn’t a bad exercise, it just doesn’t really teach you JS but just basic programming in a C-like language.

1 Like