0

let arr = [100, 123, 100, 122, 119, 203, 123, 76, 89] through JavaScript I want my output should be-

  • [122,119,203,76,89]

I tried filter method-

const arr= [100, 123, 100, 122, 119, 203, 123, 76, 89]

const removeNo = (data) => {
    return data.filter((val, ind) => data.indexOf(val) === ind);
}
console.log(removeNo(num))
Manish
  • 13
  • 3
  • what should the output if you call `console.log(removeNo(123))` ? – Srushti Shah May 12 '23 at 05:13
  • The requirement is not clear. Do you want to eliminate the number you passed in function with multiple occurences ? or do you want to eliminate all the multiple occurences of all the numbers ? or you just want to eliminate the number you passed ? – Srushti Shah May 12 '23 at 05:16
  • I want to eliminate all the multiple occurences of all the numbers. – Manish May 12 '23 at 07:26

1 Answers1

2

You could try filtering all element that their indexOf and lastIndexOf are the same value, meaning it only has one occurance in the array.

const arr= [100, 123, 100, 122, 119, 203, 123, 76, 89]

const removeNo = (data) => {
    return data.filter((val) => data.indexOf(val) === data.lastIndexOf(val));
}

console.log(removeNo(arr))

Explain

indexOf searches the index an element from the left side of an array, while lastIndexOf searches starting from the right side.

Imagine an array, that we want to find all elements without duplicates.

[1, 2, 3, 1, 2]

Using filter function, we are iterating every element throughout the array.

  1. On the first iteration, it checks the first array which is 1
[1, 2, 3, 1, 2]
 ^ <- filter pointer
  1. Then indexOf searches 1 from the left side, which is also the first element
[1, 2, 3, 1, 2]
 ^ 
 ^ <- indexOf pointer
  1. Since it found the target element from the left side, now it's time to find the same element 1 from the right side with lastIndexOf, which is the fourth element
[1, 2, 3, 1, 2]
 ^ 
 ^ 
          ^ <- lastIndexOf pointer
  1. Counting from the left side, 1 is found on the first position. But while counting from the right side, 1 is found on the fourth position. Which does not match the previous search, so the first element 1 fails the test.

  2. The second element also fails the test due to the same reason.

[1, 2, 3, 1, 2]
    ^ <- filter pointer
    ^ <- indexOf pointer
             ^ <- lastIndexOf pointer
  1. While on the third iteration, both searches lands on the same index, so it is confident to say 3 is unique in this array.
[1, 2, 3, 1, 2]
       ^ <- filter pointer
       ^ <- indexOf pointer
       ^ <- lastIndexOf pointer
  1. Then the rest of the elements also fail the test using the same logic.
[1, 2, 3, 1, 2]
          ^ <- filter pointer
 ^ <- indexOf pointer
          ^ <- lastIndexOf pointer
[1, 2, 3, 1, 2]
             ^ <- filter pointer
    ^ <- indexOf pointer
             ^ <- lastIndexOf pointer
  1. So only the third element 3 passes the test, the output is [3].

Hope this helps you understanding this approach.

Hao Wu
  • 17,573
  • 6
  • 28
  • 60
  • but can you explain it please briefly....... I am bit confused, it will more help me ... – Manish May 12 '23 at 07:27
  • @Manish I added the explaination, forgive my non-native English but hope it helps you to understand. – Hao Wu May 12 '23 at 07:47
  • Seriously man, am huge fan of yours, you literally a dope in programming, and your way of explaination is far good too. I thoroughly understand it. And in future too, more questions I am bringing in front of you. Thanks!!! – Manish May 12 '23 at 09:51