0

I have the following items. I want to remove the duplicate items and return the array. I have tried using Set but I think that is not a part of the Ecma script that I am currently using. I know this question has been asked multiples times here but I cannot seem to get mine to work.

0: (2) [0, 2]

1: (2) [0, 2]

2: (2) [1, 2]

3: (2) [1, 3]

 function checkDuplicate(array: any, obj: any) {
    const exists = array.some((o: any) => o.itemOne === obj.itemOne && o.itemTwo === obj.itemTwo);
    if (exists) {
      return true;
    } else {
      return false;
    }
  }

  function check() {
    const testArray: any = [];
    arrayOne().map((item: any) => {
      arrayTwo().map((item2: any) => {
        if (item.someMatchingValue === item2.someMatchingValue) {
          if (!checkDuplicate(testArray, [item.itemOne, item2.itemTwo])) {
            testArray.push([item.itemOne, item2.itemTwo]);
          }
        }
      });
    });
    console.log(testArray);
    return testArray;
  }
1

2 Answers 2

4

You're using const and other ES6 features, so you should be able to use a Set just fine. The issue you probably encountered was that two arrays are not equal to eath other, and so placing your array into a Set won't remove the inner arrays. Instead, you can map each inner array in your array to a string such that you can then use a Set to remove duplicates, and then use Array.from with JSON.parse to convert your Set of strings back into an array of arrays like so:

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

const res = Array.from(new Set(arr.map(JSON.stringify)), JSON.parse);
console.log(res);

Sign up to request clarification or add additional context in comments.

5 Comments

I am currently developing in react. Will this work in react aswell? Sorry I should have tagged Reactjs but thought JS was the right tag.
@Maddy I'm not a reactjs expert, but I would say that there wouldn't be an issue with this working in react.
@NickParsons An edge-case would be the use of JSON.stringify + JSON.parse converting unequal values (like undefined and null) to the same, equal value (null). Granted, I'm not sure that's a case that needs to be accounted for here.
@TylerRoper oh yeah, I didn't think about that case. I guess it is good to be aware of though. Thanks for pointing that out
Yes this would work in React and this is a very elegant solution.
0

I have this utility function I use to reduce an array to distinct elements

const distinct = (array) =>
  array
    ? array.reduce((results, item) => {
        if (!results.some(i => i === item)) {
          results.push(item);
        }
        return results;
      }, [])
    : array;
    
    
let array = [1 ,1 , 2, 3, 3, 4, 5, 5, 5];
console.log(distinct(array));

Seeing you want to compare arrays you could modify it with my object compare utility function

const compare = (obj1, obj2) =>
  Array.isArray(obj1)
    ? Array.isArray(obj2) && obj1.length === obj2.length && obj1.every((item, index) => compare(item, obj2[index]))
    : obj1 instanceof Date
    ? obj2 instanceof Date && obj1.getDate() === obj2.getDate()
    : obj1 && typeof obj1 === 'object'
    ? obj2 && typeof obj2 === 'object' &&
      Object.getOwnPropertyNames(obj1).length === Object.getOwnPropertyNames(obj2).length &&
      Object.getOwnPropertyNames(obj1).every(prop => compare(obj1[prop], obj2[prop]))
    : obj1 === obj2;

const distinct = (array) =>
    array
      ? array.reduce((results, item) => {
          if (!results.some(i => compare(i, item))) {
            results.push(item);
          }
          return results;
        }, [])
      : array;

  let array = [[0, 2], [0, 2], [1, 2], [1, 3]];
  console.log(distinct(array));
 

3 Comments

This won't work in OP's case because the duplicated elements are arrays.
^ To clarify, it will work just fine if the duplicated arrays are all references to one single array. However, two unique arrays with identical contents will not be considered equal.
I added my object compare utility function for array compare

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.