0

I have the next situation in my react application:
I have a state:

const [arr1, setArr1] = useState([1, 2, 3, 5, 1, 3]);

Bellow i render all items from array on UI like:

arr1.map(i => <li>{i}</li>)

Now i want to remove all items that are equal in the array:
ex:

  1. [1, 2, 3, 5, 1, 3] // should be deleted 1 and 3 result: [1, 2, 5]
  2. [1, 2, 3, 5, 3] // should be deleted 3 result: [1, 2, 5]
    Deleting all items also the state should change here arr1.map().
    I tried setArr1([new Set(arr1)]), but it does not delete all duplicated values, it delete just one of them.
    How to achieve what i described above?
Asking
  • 3,487
  • 11
  • 51
  • 106
  • 4
    Shouldn't 1 be removed from this `[1, 2, 3, 5, 1, 3]` ? – Hassan Imam Feb 26 '21 at 07:51
  • @HassanImam, yes, all `1` should be removed – Asking Feb 26 '21 at 07:54
  • @HassanImam It's someone who's averaging a question per hour regarding what appears to be the same issue: https://stackoverflow.com/users/12540500/asking So I'm pretty sure they themselves are thoroughly bewildered by what they want. – codemonkey Feb 26 '21 at 07:57
  • Does this answer your question? [Removing duplicate array values and then storing them \[react\]](https://stackoverflow.com/questions/37217953/removing-duplicate-array-values-and-then-storing-them-react) – iamdhavalparmar Feb 26 '21 at 08:08

4 Answers4

4

You can count the frequency of number and then just pick the number whose frequency is 1.

const arr = [1, 2, 3, 5, 1, 3],
      frequency = arr.reduce((o,v) => (o[v] = (o[v] || 0) + 1, o), {}),
      unique = Object.keys(frequency).reduce((r,k) => frequency[k] === 1? [...r, +k]: r, []);
console.log(unique);
Hassan Imam
  • 21,956
  • 5
  • 41
  • 51
  • could you take a look on this https://stackoverflow.com/questions/66381227/change-the-state-according-to-event-in-reactjs. You will help me a lot. This is something related to this question but i can't figure out how to solve. – Asking Feb 26 '21 at 08:29
1

You can calculate the frequency and remove the number if the frequency is greater than 1. codesandbox

    function removeDuplicates(arr) {
    const frequency = {};
    arr.forEach((el) => {
      frequency[el] = frequency[el] ? ++frequency[el] : 1;
    });

    const result = [];
    for (el in frequency) {
      if (frequency[el] === 1) {
        result.push(el);
      }
    }

    return result;
 }
  

    const arrayWithoutDuplicates = removeDuplicates(arr1);
      return (
        <ul>
          {arrayWithoutDuplicates.map((el) => {
            return <li key={el}> {el} </li>;
          })}
        </ul>
      );
DecPK
  • 24,537
  • 6
  • 26
  • 42
1

Check if the the first index equal to the last index on an element,when they are equals it means it is unique:

let result = []
let arr = [1, 2, 3, 5, 1, 3]

arr.forEach(e => arr.indexOf(e) === arr.lastIndexOf(e)?result.push(e):null)

console.log(result)
Alan Omar
  • 4,023
  • 1
  • 9
  • 20
0

Below is a one-liner using filter

const arr = [1, 2, 3, 5, 1, 3];

const unique = arr.filter(x => arr.filter(y => y === x).length < 2)
console.log(unique);
Owen Kelvin
  • 14,054
  • 10
  • 41
  • 74
  • Your solution has a time complexity of O(n^2). @Hassan Imam's solution has a complexity of of just O(2n). His solution is more optimised – Omkar Kulkarni Feb 26 '21 at 08:11