Skip to main content
edited tags
Link
200_success
  • 144.2k
  • 22
  • 188
  • 473
deleted 66 characters in body; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Removing duplication in JavaScriptduplicates from an array

Thanks for taking the time to review my code.

I recently wrote this JavaScript algorithm for removing duplicates from an array as part of a job interview process, but was turned down for the position after submitting the code. I didn't receive any detailed feedback from the company, so I was wondering if a more experienced developer might be able to explain what I've done wrong here.

  1. Running time is O(n^2) \$\mathcal{O}(n^2)\$ - there may be a recursive algorithm which removes duplicates more effectively than nested for loops.
  2. Code should be refactored rather than all in on big function.

WouldI would greatly appreciate any thoughts on the problems that I've identified, or other issues with my code.

Thank you.

Removing duplication in JavaScript

Thanks for taking the time to review my code.

I recently wrote this JavaScript algorithm for removing duplicates from an array as part of a job interview process, but was turned down for the position after submitting the code. I didn't receive any detailed feedback from the company, so I was wondering if a more experienced developer might be able to explain what I've done wrong here.

  1. Running time is O(n^2) - there may be a recursive algorithm which removes duplicates more effectively than nested for loops.
  2. Code should be refactored rather than all in on big function.

Would greatly appreciate any thoughts on the problems that I've identified, or other issues with my code.

Thank you.

Removing duplicates from an array

I recently wrote this JavaScript algorithm for removing duplicates from an array as part of a job interview process, but was turned down for the position after submitting the code. I didn't receive any detailed feedback from the company, so I was wondering if a more experienced developer might be able to explain what I've done wrong here.

  1. Running time is \$\mathcal{O}(n^2)\$ - there may be a recursive algorithm which removes duplicates more effectively than nested for loops.
  2. Code should be refactored rather than all in on big function.

I would greatly appreciate any thoughts on the problems that I've identified, or other issues with my code.

Source Link
jmknoll
  • 141
  • 3

Removing duplication in JavaScript

Thanks for taking the time to review my code.

I recently wrote this JavaScript algorithm for removing duplicates from an array as part of a job interview process, but was turned down for the position after submitting the code. I didn't receive any detailed feedback from the company, so I was wondering if a more experienced developer might be able to explain what I've done wrong here.

dedup = function(set){
    //loop over function, removing second instance of matching values
    for (var i = 0; i < set.length; i++){
        for (var j = i+1; j < set.length; j++){
            if (set[i] == set[j]){
                set.splice(j,1);
                i--;
            }
            //special case for objects
            if (typeof set[j] == 'object' && typeof set[i] == 'object')
                {
                //super special case for arrays
                if (Array.isArray(set[j]) && (Array.isArray(set[i])))
                { 
                    if ( set[j].toString() == set[i].toString() )
                        { set.splice(j,1);
                        i-- }
                }
                //loop over key-value pairs in sets of objects
                else {
                    var firstObj = set[i];
                    var secondObj = set[j];
                    for (var key in firstObj){
                        //break loop if differences found
                        if (firstObj.hasOwnProperty(key) == false) {
                            break
                        }
                        else if (firstObj[key] != secondObj[key]){
                            break
                            }
                        else {
                            set.splice(j,1);
                            i--
                            break
                        }
                    }
                }
            }
        }
    }
    return set
}

console.log(dedup([1, 2, 2, 5, 2, 7, 3, 9, 1, 9]))
// -> [1, 2, 5, 7, 3, 9]

console.log(dedup([1, '1', 2, 0, false, true]))
// -> [1, '1', 2, 0, false, true]

console.log(dedup([8, 3, [1, 2], [1, 2, 3], [1, 2], [2, 1], true]))
// -> [8, 3, [1, 2], [1, 2, 3], [2, 1], true]

console.log(dedup([1, {a: 'b', c: 'd'}, {c: 'd', a: 'b'}, {e: 'f'}, 'b']))
// -> [1, {a: 'b', c: 'd'}, {e: 'f'}, 'b']

I've found the following possible problems:

  1. Running time is O(n^2) - there may be a recursive algorithm which removes duplicates more effectively than nested for loops.
  2. Code should be refactored rather than all in on big function.

Would greatly appreciate any thoughts on the problems that I've identified, or other issues with my code.

Thank you.