-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingle_ele.js
35 lines (25 loc) · 929 Bytes
/
single_ele.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// Using inbuilt Methods
const arr = [1, 1, 2, 3, 3, 4, 4, 6, 7, 7, 2, 9];
const uniqueElement = arr.filter((item) => arr.indexOf(item) === arr.lastIndexOf(item))
console.log(`Unique element: ${uniqueElement}`);
const findUniqueElements = (arr) => {
const elementCount = {};
// Step 1: Count the occurrences of each element in the array
for (const element of arr) {
if (elementCount[element]) {
elementCount[element]++;
} else {
elementCount[element] = 1;
}
}
// Step 2: Collect all elements that appear only once
const uniqueElements = [];
for (const element of arr) {
if (elementCount[element] === 1) {
uniqueElements.push(element);
}
}
return uniqueElements; // Return an array of all unique elements
};
const uniqueElements = findUniqueElements(arr);
console.log(`Unique elements: ${uniqueElements}`);