As suggested by Dmitry see the second comment below a more performance wise solution would be to filter out the unique items in b before concatenating with a.
But if you use Babelyou can have it now. Here is a slightly different take on the loop. With some of the optimizations in the latest version of Chrome, it is the fastest method for resolving the union of the two arrays Chrome The top answer here with the double loop on every value i-1 is still significantly slower.
Those are by far the fastest of all results after going through almost all of the viable answers. This is a method I use a lot, it uses an object as a hashlookup table to do the duplicate checking.
Assuming that the hash is O 1then this runs in O n where n is a. I honestly have no idea how the browser does the hash, but it performs well on many thousands of data points. Implementation of indexOf method for other browsers is taken from MDC. If you want without duplicate you can try a better solution from here — Shouting Code.
New solution which uses Array. The easiest way to do this is either to use concat to merge the arrays and then use filter to remove the duplicates, or to use concat and then put the merged array inside a Set.
Following the functional approach a union of two Array s is just the composition of concat and filter. In order to provide optimal performance we resort to the native Set data type, which is optimized for property lookups. Anyway, the key question in conjunction with a union function is how to treat duplicates.
The following permutations are possible:. The first two permutations are easy to handle with a single function.
Since switching to plain old Object property lookups would entail a serious performance hit the following implementation just ignores the third and fourth permutation.
You would have to build a separate version of union to support them. It turns out unionn is just foldl aka Array.
Since 4. Returns Array : Returns the new concatenated array. The order and references of result values are determined by the first array. Array : The values to exclude. The iteratee is invoked with one argument: value. The comparator is invoked with two arguments: arrVal, othVal. Returns Array : Returns the slice of array.
Elements are dropped until predicate returns falsey. The predicate is invoked with three arguments: value, index, array.
Note: This method mutates array. Returns Array : Returns array. Since 1. Returns number : Returns the index of the found element, else Since 2. Returns Array : Returns the new flattened array. Returns Object : Returns the new object.
Combining Arrays Without Duplicates
If fromIndex is negative, it's used as the offset from the end of array. Returns number : Returns the index of the matched value, else Array : The arrays to inspect. Returns Array : Returns the new array of intersecting values.
Returns string : Returns the joined string.When developing software, we often need to combine data efficiently and without repetition. This often comes up when manipulating listed data with the use Arrays.
However, in this article, we will be taking the mostly avoided path of handling matters ourselves. We will explore 3 ways to combine the elements of multiple arrays into one without any elements getting repeated. You should already have your development environment setup for this course. Open the cloned repository and inside the Beginner folder, navigate to the mergeArrays folder. Our work for this challenge will be done in here. Given two or more arrays, write a function that combines their elements into one array without any repetition.
To achieve this, we have to somehow detect the number of arrays provided and extract them individually before merging them into one array without any element repeating itself. Since we want to create a solution that can merge any number of arrays, the first step in solving this challenge is to dynamically detect and extract the arguments arrays our function receives and merge them into one. Functions receive values with which they carry out various operations in the form of arguments that are passed to them on call.
ES6 however offers us a cleaner and more concise way to achieve the same result using the rest parameter syntax which allows us to accept an infinite number of arguments as shown below:.
Using the … syntax above, we are able to combine all the arguments passed irrespective of the number into one array which we name arrays. Our console. The next step in solving this problem is combining the arrays received into one array still containing duplicates. To do this, we use a forEach loop to iterate through each array and add the elements therein into another array which we call jointArray. For each array, we use the spread operator to spread its elements as well as the elements in the jointArray which is initially empty into a new array.
We then reassign this array to jointArray. On complete execution of this statement, jointArray now contains all the elements from all the arrays received still with duplicates. Having covered the two steps common to every solution to this problem, we shall now examine the three ways to remove the duplicates from within our jointArray.
This eliminates all duplicates as sets are designed to only hold unique values. We then spread the unique elements of the set back into an array which we return as the final result. Pretty simple, right? Another way to achieve this is by using the Array.PierBover kelabang perhaps something like:. This utilizes the ES6 rest parameters syntax This will create an array of arrays for as many array arguments you pass in.
Not to be confused with the identical looking spread syntax which is clearly used in the reducer. EliecerC FTW. EliecerC Booyaka Booyaka !! PierBover : for a generic number of arrays, an elegant way to do it :. Thumbs Up to EliecerC. All possible options, nuts for ya all!
Be aware that it's only taking into account the key you passed in your arguments to remove duplicates! One of the most important difference between the solutions, is that some methods doesn't mutate the original array i. Try this. I've noticed a quirk behavior with spread for flattening an array of arrays.
Skip to content.
The dark mode beta is finally here. Change your preferences any time. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. With Lodash, how could I remove objects with duplicate id keys? Something with filter, map and unique, but not quite sure. Assuming that the data should be unique by id and your data is stored in data variable, you can use unique function like this:.
Or simply Use unionfor simple array. It creates duplicate-free version of an array. You can also use unionBy for 4. Learn more. Lodash remove duplicates from array Ask Question. Asked 4 years, 8 months ago.
Active 5 months ago. Viewed k times. My real data set is much larger and has more keys, but the concept should be the same. ChrisRich ChrisRich 5, 8 8 gold badges 31 31 silver badges 49 49 bronze badges. Active Oldest Votes. Ziv Weissman 3, 3 3 gold badges 19 19 silver badges 50 50 bronze badges.Remove Duplicates from sorted Array
Package all your code together in a function that meets the following requirements for "correctness:".
Subscribe to RSS
Please let me know if I've made any inaccurate statements or if I can improve the above answer either for efficiency or readability. Using memoization is probably unnecessary given that the keys to the hash are going to be unique and the values are irrelevant, so this is sufficient:. I really love Object tapbut we can accomplish the same result using Enumerable reduce :. A quick peek at the source of Arraythough, seems to confirm that using a hash as an intermediary seems to be the acceptable solution to performing a unique merge between 2 arrays.
This still can cast some doubts, if you want to distinguish and ''then this would not work. Could be 21, if overwriting global values is allowed it would remove my from the code. It returns elements in random order, because order doesn't matter.
As for complexity, on average it's O N depends on number of hash collisions, but they are rather rare - in worst case it can be O N 2 but this shouldn't happen, because Perl can detect pathological hashesand changes the hash function seed when it detects such behavior. Which not only looks infinitely better but will actually compile too long a line in its golfed form with the human-readable form:.
You can use filter method to check for duplicates in array. Filter method callback takes in three arguments : current element, index of current element and the array being processed. Here is how you can use filter to remove duplicates:. As expected it would create a new array satisfying the condition specified in the callback function. The condition specified would satisfy only for unique elements and duplicates would be neglected, hence it would return an array with duplicates removed.
The uniq method returns an array after removing duplicate elements from the passed in array. From the official documentation. In order to get started with Underscore.