I have a JavaScript function that I'm working on which takes an array as input and should return another array where similar elements are grouped together in sub-arrays. The initial array is passed in, and the output should be a new array.
For instance, if I call
groupfunction([1, 2, 4, 591, 392, 391, 2, 5, 10, 2, 1, 1, 1, 20, 20])
, it ideally should give me [[1, 1, 1, 1], [2, 2, 2], 4, 5, 10, [20, 20], 391, 392, 591]
. However, the current output is [[1, 1, 1, 1], 2, [4, 4, 4], [591, 591], 392, 391, 5, 10, 20]
for some reason. What can I do to properly group identical elements into separate sub-arrays within the main array?
I came across these helpful questions while trying to resolve my issue:
- Create an array with same element repeated multiple times
- Delete zero values from Array with JavaScript
- How to create an array containing 1...N
const findnumberofinstances = (array, element) => { // This function finds how many times a specific element occurs in an array
let numberoftimes = 0;
const functionisequal = (input1) => {if(input1 === element) {return true}
else {return false}};
const indexofelement = array.findIndex(functionisequal);
while(array.includes(element) === true) {
array.find(functionisequal);
numberoftimes += 1;
array.splice(indexofelement, 1);
}
return numberoftimes;
}
const finduniquevalues = (arr) => { // This function reduces an array to only have unique values.
const map = [];
for (let value of arr) {
if (map.indexOf(value) === -1) {
map.push(value);
}
}
return map;
};
const removeElementsWithValue = (array, val) => { // This function removes specific values from an array
var i = array.length;
while (i--) {
if (array[i] === val) {
array.splice(i, 1);
}
}
return array;
}
const removezeroes = (array) => {return removeElementsWithValue(array, 0)} // This function removes zeroes from an array
const repeatelement = (elem, n) => { // This function repeats an element, effectively multiplying an array of one value [value] by n so that value occurs n times.
var arr = [];
for (var i = 0; i < n; i++) {
arr = arr.concat(elem);
};
return arr;
};
const findnumberofeachelement = (array) => {let blank = []; // This function finds how many of each element is in an array.
let arraycopy = array.concat([]);
let sortedArray = array.slice().sort();
let arraycopysorted = arraycopy.slice().sort();
for(element of arraycopysorted){let num = findnumberofinstances(sortedArray, element);
blank.push(num);
}
return removezeroes(blank);
}
const groupfunction = (array) => { // This function groups elements that are identical into sub-arrays only if there are more than one of the elements.
let sum = [];
let unique = finduniquevalues(array);
let blank = [];
let number = findnumberofeachelement(array);
for(let i=0; i< unique.length; i++) {
if(number[i] > 1) {
sum = repeatelement(unique[i], number[i])
blank.push(sum);
}
else {
sum = unique[i]
blank.push(sum)
}
sum = []
}
return blank
}
console.log(groupfunction([1, 2, 4, 591, 392, 391, 2, 5, 10, 2, 1, 1, 1, 20, 20])) // This should return [[1, 1, 1, 1], [2, 2, 2], 4, 5, 10, [20, 20], 391, 392, 591].