Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to find uncommon elements in two arrays - JavaScript
Finding uncommon elements in two arrays means identifying elements that exist in one array but not in both. This is also known as finding the symmetric difference between two arrays.
Problem Definition
Given two arrays, we need to find elements that are present in either the first array or the second array, but not in both arrays.
const arr1 = [12, 54, 2, 4, 6, 34, 3];
const arr2 = [54, 2, 5, 12, 4, 1, 3, 34];
console.log("Array 1:", arr1);
console.log("Array 2:", arr2);
Array 1: [12, 54, 2, 4, 6, 34, 3] Array 2: [54, 2, 5, 12, 4, 1, 3, 34]
Method 1: Using indexOf() with Loops
This approach checks each element of both arrays to see if it exists in the other array. If not found, it's added to the result array.
const arr1 = [12, 54, 2, 4, 6, 34, 3];
const arr2 = [54, 2, 5, 12, 4, 1, 3, 34];
const unCommonArray = (first, second) => {
const res = [];
// Check elements in first array
for(let i = 0; i < first.length; i++){
if(second.indexOf(first[i]) === -1){
res.push(first[i]);
}
}
// Check elements in second array
for(let j = 0; j < second.length; j++){
if(first.indexOf(second[j]) === -1){
res.push(second[j]);
}
}
return res;
};
console.log("Uncommon elements:", unCommonArray(arr1, arr2));
Uncommon elements: [6, 5, 1]
Method 2: Using filter() and includes()
A more modern approach using array methods for cleaner, more readable code.
const arr1 = [12, 54, 2, 4, 6, 34, 3];
const arr2 = [54, 2, 5, 12, 4, 1, 3, 34];
const findUncommonElements = (first, second) => {
const firstUnique = first.filter(item => !second.includes(item));
const secondUnique = second.filter(item => !first.includes(item));
return [...firstUnique, ...secondUnique];
};
console.log("Uncommon elements:", findUncommonElements(arr1, arr2));
Uncommon elements: [6, 5, 1]
Method 3: Using Set for Better Performance
Using Set provides O(1) lookup time compared to O(n) for indexOf() or includes().
const arr1 = [12, 54, 2, 4, 6, 34, 3];
const arr2 = [54, 2, 5, 12, 4, 1, 3, 34];
const findUncommonWithSet = (first, second) => {
const set1 = new Set(first);
const set2 = new Set(second);
const result = [];
// Elements in first but not in second
for(let item of set1) {
if(!set2.has(item)) {
result.push(item);
}
}
// Elements in second but not in first
for(let item of set2) {
if(!set1.has(item)) {
result.push(item);
}
}
return result;
};
console.log("Uncommon elements:", findUncommonWithSet(arr1, arr2));
Uncommon elements: [6, 5, 1]
Comparison
| Method | Time Complexity | Readability | Best For |
|---|---|---|---|
| indexOf() with loops | O(n²) | Medium | Small arrays |
| filter() with includes() | O(n²) | High | Readable code |
| Set-based approach | O(n) | Medium | Large arrays |
Conclusion
Use the Set-based approach for better performance with large arrays, or the filter() method for more readable code with smaller datasets. All methods successfully identify uncommon elements between two arrays.
