Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 months ago.
Improve this question
What's up guys, I was just messing arround LeetCode, and different ways to solving this problem (I know the Set way and the for of with array.includes + push, and the filter, but all of them create a new array), and with this method I'm getting this output, can someone explain me why?
P.S. You can't create a new array, just modify the first one.
let nums = [0, 0, 1, 1, 1, 2, 2, 3, 3, 4];
var removeDuplicates = (nums) => {
for (let i = 0; i < nums.length; i++) {
if (nums[i] === nums[i + 1]) {
nums.splice(i, i + 1);
}
}
};
removeDuplicates(nums);
console.log(nums);
// [0, 1, 3, 4]
The first problem is here: nums.splice(i, i + 1);
Array splice method takes 2 arguments, the first one is the index of element, and the second one is a number of elements to replace/remove (it seems you confused the second argument with the end index). Source: Array.prototype.splice()
Another problem is changing the original array, while iterating over it (be very careful with such in-place manipulations, they very often result in some kind of errors). Look at what is happening in your loop:
You have some array [0, 0, 0, 1, 1, 2, 2];
You want to remove the first element, since it is a duplicate: [0, 0, 0, 1, 1, 2, 2];
After removing, you have [0, 0, 1, 1, 2, 2];
You increment your i, so now it is equal to 1;
You look at the element with index 1, and find no duplicates [0, 0, 1, 1, 2, 2], since you just skipped the element, that moved into the first index in place of the removed element.
The solution is to decrement the i after removing the element:
let nums = [0, 0, 1, 1, 1, 2, 2, 3, 3, 4];
var removeDuplicates = (nums) => {
for (let i = 0; i < nums.length; i++) {
if (nums[i] === nums[i + 1]) {
nums.splice(i, 1);
i--;
}
}
};
removeDuplicates(nums);
console.log(nums);
// [0, 1, 2, 3, 4]
As you say, the job of the splice method is to modify the contents of the array.
The two args you're passing it are the start position and the delete count.
You also need the replacement value, and to keep replacing until that condition is not longer true, so the while loop is also an option:
let nums = [0, 0, 1, 1, 1, 2, 2, 3, 3, 4];
var removeDuplicates = (nums) => {
for (let i = 0; i < nums.length; i++) {
while (nums[i] === nums[i + 1]) {
nums.splice(i, 2, nums[i]);
}
}
};
removeDuplicates(nums);
console.log(nums);
Output:
[ 0, 1, 2, 3, 4 ]
Related
I am learning JavaScript right now and I want to practice for loops and if statements. So I found a little exercise: Remove all duplicates in the array. I know there a probably better solutions but I want to know why my code isn't working. Can someone help me? Thx :)
var arr = [1, 1, 2, 3, 3, 3, 4, 5, 5];
var b = 0;
function arrRemover() {
for (i = 0; i < arr.length; i++) {
b++
if (arr[b] == arr[b + 1]) {
arr.splice[b + 1];
}
}
return arr;
}
console.log(arrRemover());
splice is a method so you have to call that method with specific arguments.
You don't need b here
Better not to loop over that array from start instead loop from backward so that you won't skip over elements when you remove any element from an array beause if an elment gets deleted and you then counter increment and skip some elements
var arr = [1, 1, 2, 3, 3, 3, 4, 5, 5];
function arrRemover() {
for (let i = arr.length - 1; i > 0; i--) {
if (arr[i] === arr[i - 1]) {
arr.splice(i, 1);
}
}
return arr;
}
console.log(arrRemover());
You can also use Set here as:
var arr = [1, 1, 2, 3, 3, 3, 4, 5, 5];
function arrRemover() {
return [...new Set(arr)];
}
console.log(arrRemover());
I am just starting to learn js and as many (I'm sure) I am facing problems lol. I am trying to copy an array into another array at a designated index location.
I seem to be able to do everything correct but when I log the result, the inserted array shows in reverse and I have no idea why¿?¿?.
Have also tried a .forEach as looping method, but I'm still a bit green to use it right.
This is the code:
function frankenSplice(arr1, arr2, n) {
let newArr = arr2.slice([]);
for (let i = 0; i < arr1.length; i++) {
newArr.splice(n, 0, arr1[i]);
console.log(newArr)
}
}
frankenSplice([1, 2, 3], [4, 5, 6], 1);
It logs // [ 4, 3, 2, 1, 5, 6 ] instead of the desired out: [ 4, 1, 2, 3, 5, 6 ]
You are inserting the elements at the same index, so after inserting 1 you insert 2 at the same position, shifting back the 1 and all following elements.
Either iterate backwards over arr1 or increment n after each iteration like I have done here.
function frankenSplice(arr1, arr2, n) {
let newArr = arr2.slice([]);
for (let i = 0; i < arr1.length; i++) {
newArr.splice(n, 0, arr1[i]);
console.log(newArr)
n++; // add this
}
}
frankenSplice([1, 2, 3], [4, 5, 6], 1);
This question already has answers here:
Looping through array and removing items, without breaking for loop
(17 answers)
Closed last year.
I need to write a function
filterRangeInPlace(arr, a, b)
that takes an array and two numbers and deletes all elements from the array that are not within the range a to b. So the check looks like
a ≤ arr[i] ≤ b
I want to do it with the for loop and splice method. But can't figure out why the function keeps some elements outside of the range, in my case below it's number 3. This is not a home task or whatever, I'm just practicing array methods. Here's my code:
let myArr = [1, 3, 8, 3, 9, 5, 3, 4, 10, 7, 6, 1]
function filterRangeInPlace( arr, a, b ) {
for ( i = 0; i < arr.length; i++ ) {
if ( arr[i] < a || arr[i] > b ) {
arr.splice(i, 1)
}
}
}
filterRangeInPlace(myArr, 4, 9)
console.log(myArr) // [3, 8, 9, 5, 4, 7, 6]
I understand I messed with the index somewhere, but can't figure out where and how, as the rest works fine. Thanks!
You should start iterating from the end of the array if you are using splice here and go upto the first element as:
for (i = arr.length - 1; i >= 0; i--) {
Let say, you are removing the first element i.e. 1 at position 0 then after removing the array will be
[3, 8, 3, 9, 5, 3, 4, 10, 7, 6, 1]
and at that time the i increments and becomes 1, so now it will point to 8 as 1 index.
So using splice will skip some of the steps
let myArr = [1, 3, 8, 3, 9, 5, 3, 4, 10, 7, 6, 1];
function filterRangeInPlace(arr, a, b) {
for (i = arr.length - 1; i >= 0; i--) {
if (arr[i] < a || arr[i] > b) {
arr.splice(i, 1);
}
}
}
filterRangeInPlace(myArr, 4, 9);
console.log(myArr);
When you use splice, and you're still in the same loop, you skip some indexes (because splice deletes number of items, so the indexes are changed).
In your example, the 1, which is first, is deleted, so 3 becomes the first one, but then you skip to the second (i++ in the loop). You don't double check the same index.
I'd recommend using filter method to do that, or just putting i-- on the line after splice.
I'm trying this quiz on Codewars: Smallest unused ID
You've got much data to manage and of course you use zero-based and non-negative ID's to make each data item unique!
Therefore you need a method, which returns the smallest unused ID for your next new data item...
Note: The given array of used IDs may be unsorted. For test reasons there may be duplicate IDs, but you don't have to find or remove them!
In short, You are given an array. What the quiz requires is to find the smallest unused number.
For example:
[0,1,2,3,5] // Output: 4
[1,2,3,5] // Output: 0
[0,1,2,3,4,5] // Output: 6
[0,0,0,0,0,0] // Output: 1
[0,1,0,2,0,3] // Output: 4
My code can't pass some tests. Inputs for the tests are hidden, so I am unable to figure out the causes. What is causing the issue?
const nextId = ids => {
let lowestId;
if (Math.max(...ids) + 1 === ids.length) {
lowestId = Math.max(...ids) + 1;
} else {
let sortedIds = ids.sort((a, b) => a - b);
for (let i = 0; i < sortedIds.length; i++) {
if (i !== sortedIds[i]) {
lowestId = i;
break;
}
}
}
return lowestId
}
console.log(nextId([0, 1, 2, 3, 5])) // 4
console.log(nextId([1, 2, 3, 5])) // 0
console.log(nextId([0, 1, 2, 3, 4, 5])) // 6
console.log(nextId([0, 0, 0, 0, 0, 0])) // 1
console.log(nextId([0, 1, 0, 2, 0, 3])) // 4
You're over complicating it
function doStuff(stuff)
{
for(let i = 0; i < stuff.length + 1; i++)
{
if(stuff.indexOf(i) == -1)
return i;
}
}
It looks like you missed this line of the instructions:
"Note: The given array of used IDs may be unsorted. For test reasons there may be duplicate IDs, but you don't have to find or remove them!"
With the logic of your code, if there's a duplicate then the value won't match the index. You need to actually compare the values to account for duplicates.
function nextId(ids) {
let lowestId = 0;
let sortedIds = ids.sort((a, b) => a - b);
for (let i = 0; i < sortedIds.length; i++) {
if (lowestId === sortedIds[i] && lowestId !== sortedIds[i + 1]) {
lowestId++;
}
else if (lowestId !== sortedIds[i + 1]) {
return lowestId;
}
}
return lowestId
}
This approach takes an object for seen values.
The start node is -1, because the first missing id could be zero.
Performance:
To collect all nodes: O(n).
To find the missing smallest id: smaller than O(n).
function nextId(ids){
const seen = {};
let id = -1;
for (const id of ids) seen[id] = true;
while (seen[++id]) ;
return id;
}
console.log(nextId([0, 1, 2, 3, 5])); // 4
console.log(nextId([1, 2, 3, 5])); // 0
console.log(nextId([0, 1, 2, 3, 4, 5])); // 6
console.log(nextId([0, 0, 0, 0, 0, 0])); // 1
console.log(nextId([0, 1, 0, 2, 0, 3])); // 4
I am begginer at JavaScript and currently training at Code Wars, and I have some problems with my code here. I got a task to find a number in a given array, that differs from the other numbers in the same array. When I run the tests only at those two specific arrays the code returns 'undefined'. So I wanted to know why and how should I improve my code? Maybe I missed something.
Appreciate your help.
Here is my code:
function findUniq(arr) {
for (let i = 0; i < arr.length; i++) {
if (arr[i] !== arr[0] && arr[i] !== arr.slice(-1)[0]) {
return arr[i];
}
}
}
So I have those two arrays:
findUniq([0, 1, 1, 1, 1, 1, 1, 1]); expected 0, but returns undefined.
findUniq([8, 8, 8, 8, 8, 8, 8, 7]); expected 7, also returns undefined.
You can also use reduce to club digits as key-value pair and than find the key which has value 1
function findUniq(input){
let op = input.reduce((op,inp)=>{
if(op[inp]){
op[inp]++;
} else {
op[inp] = 1
}
return op
},{})
return Object.keys(op).find(e=> op[e] === 1)
}
console.log(findUniq([0, 1, 1, 1, 1, 1, 1, 1]))
console.log(findUniq([8, 8, 8, 8, 8, 8, 8, 7]))
For every element in the array you check wether it unequals the first and the last position:
//❌ ✔️
//🔽 🔽
[0, 1, 1, 1, 1, 1, 1, 1]
//⬆️
Now for the first element (which is the one to find), comparing to the last element will result in "unequal", because 0 is not 1, but checking for the first will result in "equal" as you compare the elementto itself.
For it to detect the first and last element, don't check if it unequals both the first and the last, but wether it unequals the previous and the next, then jump back to the end or beginning when the index leaves the array.
Through that it also works for the last and the first element:
// ✔️ ✔️
// 🔽 🔽
[0, 1, 1, 1, 1, 1, 1, 1]
//⬆️
Perhaps the simplest way is, as Jonas Wilms suggested, to find the first one that is not equal to both its predecessor and its successor, wrapping around at the end of the list:
const findUnique = arr => arr.find((n, i, a) =>
n !== a[(i - 1) % arr.length] && n !== a[(i + 1 % arr.length)]
)
console.log(findUnique([1, 1, 3, 1, 1, 1, 1, 1])) //~> 3
console.log(findUnique([0, 1, 1, 1, 1, 1, 1, 1])) //~> 0
console.log(findUnique([8, 8, 8, 8, 8, 8, 8, 7])) //~> 7
Note that this uses %, the remainder or modulus operator of Javascript to handle sliding off the end of the list.
function findUniq(arr) {
for (let i = 0; i < arr.length; i++) {
if (arr[i] !== arr[arr.length - (i-1)] && arr[i] !== arr[arr.length + (i-1)] && arr[i] !== arr[i+1] && arr[i] !== arr[i-1]) {
return arr[i];
}
}
}
Well here is my own answer thanks to everyone, I will take to attention any critics you have, to improve.Thanks to Jonas Wilms:)