Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function cleanTheRoom(arr) {
- // sort the array in ascending order
- arr.sort((a, b) => a - b);
- // initialize an empty array to store the result
- let result = [];
- // initialize a variable to store the current element
- let current = null;
- // initialize a variable to store the count of the current element
- let count = 0;
- // loop through the array
- for (let num of arr) {
- // if the current element is null or different from the num
- if (current === null || current !== num) {
- // if the count is more than one, push an array of the current element and count to the result
- if (count > 1) {
- result.push(Array(count).fill(current));
- }
- // else if the count is one, push the current element to the result
- else if (count === 1) {
- result.push(current);
- }
- // update the current element and count
- current = num;
- count = 1;
- }
- // else if the current element is equal to the num, increment the count
- else {
- count++;
- }
- }
- // handle the last element and count
- if (count > 1) {
- result.push(Array(count).fill(current));
- } else if (count === 1) {
- result.push(current);
- }
- // return the result
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement