0

I have the following C# code which solves the problem using the Except() function:

List<String> origItems = new List<String>();
origItems.Add("abc");
origItems.Add("def");
origItems.Add("ghi");

List<String> newItems = new List<String>();
newItems.Add("abc");
newItems.Add("def");
newItems.Add("super");
newItems.Add("extra");

List<String> itemsOnlyInNew = newItems.Except(origItems).ToList();
foreach (String s in itemsOnlyInNew){
    Console.WriteLine(s);
}

The output for this will be:

super

extra

The result is the two items from the 2nd list which are not contained in the 1st list.

Using Modern JavaScript

If I have two arrays in JavaScript

let origItems = ['abc','def','ghi'];
let newItems = ['abc','def','super','extra'];

What would be the most efficient & clearest way to solve this problem?

Is there a method simlar to C# Except?
Or is it necessary to iterate over every item in the first array for each item in the 2nd array?

Additional Constraint for Understanding the Problem

The original arrays are not necessarily pre-sorted and each can be in any order. They just happen to be that way in the data. That means if part of your solution is that they are sorted then it must show that in the answer.

raddevus
  • 7,113
  • 5
  • 61
  • 73
  • 1
    There's no built-in method for this in JS. – Barmar Jan 21 '22 at 19:54
  • In JavaScript, there does seem to be something that works in quite a similar way. Here's the entire solution in JavaScript : `let origItems = ['abc','def','ghi'];` `let newItems = ['abc','def','super','extra'];` `var difference = newItems.filter(x => origItems.indexOf(x) === -1);` `console.log(difference);` – raddevus Jan 21 '22 at 21:24
  • Yes, but you asked if there was something that didn't require you to write the loop and comparisons. – Barmar Jan 21 '22 at 21:25
  • What you wrote should be in the linked question. – Barmar Jan 21 '22 at 21:27

0 Answers0