-2

Edit: Please read the question before closing it! The suggested answer has zero to do with what I asked. I'm not after a single array solution, I'm using two arrays...


I'm deduplicating an array but when I go to find the difference it doesn't find any because whats in the new array is in the old array:

const original = [1,2,2,3,4]

const newArr = Array.from(new Set(original).values());

const diff = original.filter(line => !newArr.includes(line));

What I expect to find is that the difference found is 2. In my real example I'm using crypto wallet addresses, and I need to deduplicate the list, but I also want to print to stdout what the differences are between the original array and the new array.

Example

Given the below input

123asd123asd123
123asd123asd123
890wer890wer809

I expect the following output

Deduplication complete!

Wallet 123asd123asd123 - Duplicate removed
Ari
  • 5,301
  • 8
  • 46
  • 120
  • @VLAZ I don't see any typescript in the linked dupe... – spender May 10 '22 at 11:05
  • 4
    @spender And what is the problem? – VLAZ May 10 '22 at 11:06
  • @VLAZ lol did you close this? Did you read the Q at all? How does the linked answer solve this. – Ari May 10 '22 at 11:07
  • 3
    You want to find duplicated values, do you not? – VLAZ May 10 '22 at 11:08
  • @VLAZ Given the typescript tag on this question, it seems to me that the OP might be looking for a typescript solution (and also to be able to identify the dupes rather than just discarding them) – spender May 10 '22 at 11:09
  • @VLAZ No. I want to find the difference between an array (with duplicates) and an array I have deduplicated. – Ari May 10 '22 at 11:09
  • 4
    @spender TS is a superset of JS. I fail to see how a TS solution would be in any way different to a JS one. The operations will be the same. There would only be some type annotations extra for TS. – VLAZ May 10 '22 at 11:11
  • @Ari I've swapped the duplicate target. – VLAZ May 10 '22 at 11:12
  • @VLAZ Hoping to see some TS answers, but will make this work. My next question is going to be "How to make this work in TS" btw :) – Ari May 10 '22 at 11:16
  • 5
    ...Again, TS is a superset of JS. Anything written in JS is *already written in TS*. If you have compiler options that require type annotations, you'd [need to add them](https://tsplay.dev/Nr5qDw). – VLAZ May 10 '22 at 11:19
  • There is a bevy of questions covering both aspects of this already on Stack Overflow. The first has been addressed with the duplicate target(s). The second can be found by googling, or clicking some of these links: https://stackoverflow.com/questions/9736804/find-missing-element-by-comparing-2-arrays-in-javascript ; https://stackoverflow.com/questions/49094261/compare-two-arrays-and-find-items-that-are-missing-in-second-array ; https://stackoverflow.com/questions/1187518/how-to-get-the-difference-between-two-arrays-in-javascript – TylerH May 12 '22 at 18:19

1 Answers1

0

Answering my own question. What I wanted to find was the difference between the original array and the array after it was deduplicated so I can print to the user which items were removed. Using the suggested or existing answers from other questions only deduplicates and/or don't account for duplicates and therefore fail to detect them.

My current solution

const original = [
    "apple",
    "apple",
    "banana",
    "orange",
    "apple",
]

// After deduplication, code omitted for clarity
// deduped = Array.from(new Set(original).values());
const deduped = [
    "banana",
    "orange",
    "apple",
]

// Now lets find the differences
let diff = original.slice()
for (let i = 0; i < deduped.length; i++) {
    const item = deduped[i];
    if (diff.includes(line)) {
        diff.splice(diff.indexOf(line), 1);
    }
}

// Display the difference of what is not in the deduced array
console.log(diff);

output

["apple", "apple"]

The above works in TS. No, it doesn't contain type annotations, but also doesn't require adding them either like other answers. May not be the most succinct my this solved my issue.

I am using this for a format function, where I want to see all changes made between two text files by line.

Ari
  • 5,301
  • 8
  • 46
  • 120
  • I guess I don't understand why [this](https://tsplay.dev/Wy496m) doesn't work for you. – VLAZ May 13 '22 at 09:46