0

im working my way through the odin project and looking at for loops and different methods. Im trying to find a way to pass this test :

test('removes multiple of the same value', () => {
expect(removeFromArray([1, 2, 2, 3], 2)).toEqual([1, 3]);
});

I know you can remove a duplicate using filter and Sets but how do i remove both the 2's in the array so i end up with [1, 3]

I know this is probably simple but its just where im upto at the moment.

2
  • What language are you working in? You don't say and Odin offers several. The solution may depend on the language you are using.
    – Stephen P
    Commented Jul 9 at 23:03
  • Usually the tag "algorithm" is reserved for questions for which the method of solution is paramount, regardless of the language being used. Here you specifically want a Javascript solution to a common coding problem. so I think the "algiorithm" tag is misplaced. Having misleading tags is not costless, as it wastes the time of members (such as myself) who have set up filters that would have excluded the question from those brought to their attention had the "algorithm" tag not been present. The amount of time wasted may be small for each misguided user but may be substantial in the aggregate. Commented Jul 10 at 2:09

3 Answers 3

1

You can iterate twice over the array. In the first iteration, you count the occurrences of the elements and in the second iteration you remove elements with occurrence > 1.

-1
function removeFromArray(arr) {

   if(!arr || !arr.length){
      return [];
   }
   const remElem = {};

   const all = arr.reduce((all,cur) => {
   if(!all[cur]) {
      all[cur] = true;
   } else {
      remElem[cur] = true;
   }
      return all;
   }, {}) ;

   return Object.keys(all)
      .filter((cur) => !remElem[cur])
      .map((cur) => Number(cur));
} 

Wrote on cellphone, cant test, please test, it should work :)

Added: Stackblitz Demo

3
  • Thanks, this also works however at the stage i'm at with JS I cant grasp the working parts. Thankyou for your assistance all the same. Commented Jul 9 at 23:04
  • Not my downvote, but this code doesn't work for three reasons. It crashes with TypeError: all is undefined and the goal is to remove elements from an array. That usually means in-place, keep the original data type and keep the order. Your code creates a new array, converts all numbers to strings and changes [2, 1, 3] to ["1", "2", "3"]. The OPs answer uses .splice which modifies the original array in-place, keeps the order of the other elements and keeps the data structure.
    – jabaa
    Commented Jul 10 at 7:18
  • I just added a Stackblitz. I forgot about the return all. Also thanks for mentioning the type thing, I fixed that too. It's doing the job now. It was not mentioned that the original array has to be used.
    – jaheraho
    Commented Jul 10 at 11:37
-2

Ok I came up with this :

const array = [1, 2, 2, 3];

function removeFromArray(){
 
let uniqueArray = [...new Set(array)];
uniqueArray.splice(1,1);
return uniqueArray;

  
}

removeFromArray();

It passed the test.Not sure its the right way to do things but it works and i understand it so thats a bonus.

Jabaa, thanks for your prompt. Sometime with JS I just cant think straight.

2
  • 1
    it only works with the case of [1, 2, 2, 3] or similar, you can't hard code the index to remove if you want it to work with dynamic data. Commented Jul 10 at 1:24
  • Exactly what Lã Ngọc Hải said. In this case a simple new Set[1,2,2,3] doesn't work, because it just removes duplicates, but not the "first" value. So the result would be [1,2,3].
    – jaheraho
    Commented Jul 10 at 5:37

Not the answer you're looking for? Browse other questions tagged or ask your own question.