0

How can i delete all duplicates in array without using Set (because it will disturb the order)

And why my loop with splice method doesnt work?

let result = [
  'j', 'a', 'a', 'v',
  'a', 'a', 's', 'c',
  'r', 'i', 'p', 't'
  ]
;


for (let i = 0; i < result.length; i++) {
        if (result[i] === result[i + 1]){
            result.splice(result.indexOf(result[i]), 1);
        }
}

console.log(result)  //[  "j", "v", "a",  "a",  "s",  "c", "r", "i",  "p",  "t" ]

expected output - > [  "j", "a",  "v",  "a",  "s",  "c", "r", "i",  "p",  "t" ]
3
  • 2
    "without using Set (because it will disturb the order)" sets preserve the insertion order. Commented Feb 24, 2021 at 9:58
  • "because it will disturb the order" It won't, tho Commented Feb 24, 2021 at 9:58
  • The text and the expected output are conflicting. Do you actually mean you want to remove all the sequential duplicates instead of all duplicates? Commented Feb 24, 2021 at 11:29

3 Answers 3

1

You could filter the array and have a look to the predecessor.

const
    data = ['j', 'a', 'a', 'v', 'a', 'a', 's', 'c', 'r', 'i', 'p', 't'],
    result = data.filter((v, i, a) => v !== a[i - 1]);

console.log(...result);

Sign up to request clarification or add additional context in comments.

4 Comments

most probably the solution wanted (javascript :) , but technically this duplicates 'a' so not the solution asked
@malarres, but it returns the wanted pattern.
@malarres No? At least the result is the same as OP's expected output. I suppose they actually wanted to ask how to remove sequential duplicates.
OP question: How can i delete all duplicates in array without using Set (because it will disturb the order) but yes you're right expected output - > [ "j", "a", "v", "a", "s", "c", "r", "i", "p", "t" ]
0

Your method fails in your condition:

if (result[i] === result[i + 1])

Because you only check to see if the next item is the same as the current item and not against the entire array.

To solve this you can create a new array and push in this array only the values that are not already there, thus eliminating duplicates.

const result2 = [];
for (let i = 0; i < result.length; i += 1) {
    if (result2.indexOf(result[i])>-1) {
    continue;
  }
  result2.push(result[i]);
}

Comments

0

I think to remove duplicate from an array write a function that do the job and return the array with unique item in it. Please find the below code that serve the propose.

function removeDuplicate(data) {
const tempArray = [];
data.forEach( (item) => {
  if (tempArray.indexOf(item) === -1) {
    tempArray.push(item);
  }
});
return tempArray;

}

let result = [ 'j', 'a', 'a', 'v', 'a', 'a', 's', 'c', 'r', 'i', 'p', 't' ];

result = removeDuplicate(result );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.