Seek and Destroy, output is correct but the order is meesed up

Tell us what’s happening:

As per the question i should only remove the element that passed with the array… the output is correct the only thing stopping it from passing is that the order of output is not matching.

The question does not say to maintain the order anywhere

eg:
destroyer([1, 2, 3, 5, 1, 2, 3], 2, 3)
the test expect to return [1,5,1] but my code [1,1,5]

destroyer(["possum", "trollo", 12, "safari", "hotdog", 92, 65, "grandma", "bugati", "trojan", "yacht"], "yacht", "possum", "trollo", "safari", "hotdog", "grandma", "bugati", "trojan")

the test expect [12,92,65] but my code [12,65,92]

  **Your code so far**
function destroyer(arr) {

const obj = {}

arr.forEach(value => {
  obj[value] = obj[value] ? ++obj[value] : 1
})



const deleteEl = Array.from(arguments).slice(1)


deleteEl.forEach(value => {
  delete obj[value]
})

let newArr = []

console.log(obj)

for (let key in obj) {
  let toStore = parseInt(key)

  if (typeof toStore === 'number' && !isNaN(toStore)) {
    newArr = newArr.concat(Array(obj[key]).fill(toStore))
  } else {
    newArr = newArr.concat(Array(obj[key]).fill(key))
  }



}



console.log(newArr);
return newArr;
}

destroyer(["possum", "trollo", 12, "safari", "hotdog", 92, 65, "grandma", "bugati", "trojan", "yacht"], "yacht", "possum", "trollo", "safari", "hotdog", "grandma", "bugati", "trojan");

// destroyer([2, 3, 2, 3], 2, 3);

// destroyer(["tree", "hamburger", 53], "tree", 53)
  **Your browser information:**

User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:101.0) Gecko/20100101 Firefox/101.0

Challenge: Seek and Destroy

Link to the challenge:

[12,92,65] is not the same as [12,65,92]. In arrays, order matters.

An array is an ordered list, meaning that order matters, and changing the order of elements is an unexpected effect.

I wanted to write the solution in a different way, in my approach there wasnt a way to persist the order…

Don’t get me wrong - you can use arrays in a way where you don’t care about the order. There is nothing wrong with it, per se. To be honest, if I were confronted with a problem like this, that would be one of my first questions. But I probably would assume that the order should be preserved. The instructions say, “Remove all elements from the initial array that are of the same value as these arguments.” So, it is the same array but with those values removed. To me, that means that the order is the same. And if nothing else, that would be my first guess once the tests failed.

Just on a coding level, your method is very complex. There are much simpler ways to solve this.

1 Like

I try to solve problem with different approaches this gives me chance to play around alot and i also try to use Map, Set and obj alot… it helps in avoiding nested for loops…

it is an overkill as js is preety fast if you just want to loop over few hundred elements…

I will keep this in mind, next time haha there is always something to learn…

But a lot of those are just going to use loops behind the scene, or create hash tables or other data structures that use up memory. There ain’t no such thing as a free lunch.

Indeed, and as I added in my previous comment, its an overkill if you are just looping over few hundred elements but if you increase the element you will see a significant drop in performance where as with object approach you will have much more performant code as accessing element in obj is just constant time…

So, here what i think, in the end of day a working solution matters… optimizing a code is not that hard…

Right but in saving time, you are using up a lot more memory and creating more complex, more difficult to maintain code.

accessing element in obj is just constant time

Only for a perfectly optimized hash table. Worst case it is O(n). In reality it is probably pretty close to O(1), but people like to assume that it is constant time. But in reality you will have some hashes that have more than one element in them so you will have some kind of a loop, albeit much, much smaller.

People obsess too much about micro-optimization. 99% of the time they are unnecessary, especially in a web environment where the computer spends most of its time waiting for input. As the old saying goes, “Premature optimization is the root of all evil.” I’ve seen a lot of learners go off on unproductive tangents trying to micro-optimize in ways they didn’t understand for code that would never need it. If this code came across my desk I would reject it. If this were submitted by an applicant, I would not hire them. This is a Rube Goldberg machine when all you needed was a pair of needle nose plyers.

1 Like

Interesting, thank you for great insight… i will try not over complicate if things can be solved using high order function and simple loop…

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.