Click here to Skip to main content
15,887,477 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
JavaScript
var removeDuplicates = function (nums) {
  for (let i = nums.length- 1; i >= 2; i--)

    if (nums[i - 2] == nums[i]) nums.splice[i, 1];
}; 



Output
output  [1,1,1,2,2,3] 

expected   [1,1,2,2,3]


What I have tried:

My question is that its is the not a complex data but is not define i dont know ..
please solve this questions
Posted
Updated 9-Nov-23 7:13am
v2
Comments
Member 15627495 9-Nov-23 3:13am    
https://flexiple.com/javascript/find-duplicates-javascript-array

if you need few methods , read this page

It's not clear what problem you're having, but the simplest way to remove duplicates in JavaScript is to use a Set[^]:
JavaScript
const removeDuplicates = nums => [...new Set(nums)];

However, the title and function name don't match your expected output, which clearly has duplicates in it. And since you haven't given us the input you use to produce that expected output, we can't even begin to guess what you're actually trying to do.
 
Share this answer
 
Your problem in your code is the use of '[]' instead of using '()' -

JavaScript
//Example array with duplicates using your given expected output...
let nums = [1, 1, 1, 2, 2, 3];

var removeDuplicates = function(nums) {
  for (let i = nums.length - 1; i >= 2; i--)
    if (nums[i - 2] == nums[i]) nums.splice(i, 1); //Use () instead of []...
};

//Call the removeDuplicates function...
removeDuplicates(nums);

//Output your modified array...
console.log(nums);

//OUTPUT - [1, 1, 2, 2, 3]...


I have created a fiddle for you - Javascript remove duplicates in a set of numbers[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900