This code snippet offers a unique approach to solving the given problem.
const data = [
[1,1,2,1,3],
[2,1,4,4,5],
[3,1,4,5,1]
];
// Flattening the two-dimensional array into a single dimension
const transformedArray = data.flat();
const uniqueValuesArray = [];
const duplicateCounts = [];
// Create an array with only unique values
transformedArray.forEach(item => {
if(!uniqueValuesArray.includes(item)) {
uniqueValuesArray.push(item);
}
});
// Count the occurrences of each unique element
uniqueValuesArray.forEach(uItem => {
const count = transformedArray.filter(tItem => tItem === uItem).length;
duplicateCounts.push({
element: uItem,
count: count
});
});
// Testing the output
console.log(transformedArray);
console.log(duplicateCounts);