1

My scenario, I am trying to remove array of string value duplication and print only unique value of index. For example:

const numbers =  [
"3445612712345678", 
"3445613787654321", 
"3445615712345678", 
"3445618787654321", 
];

var uniqueValue = numbers.filter((v, i, a) => a.indexOf(v) === i);
console.log(uniqueValue);

If you seen, my array of string values first 5 characters are same for all index then next two values will be changing but after those two from 8th position of string values are matching one to one, here I have to do matching from 8th position to till end then need to print unique index of values.

[
"34456 12 712345678",  // Same(1) 712345678 // Add
"34456 13 787654321",  // Same(2) 787654321 // Add
"34456 15 712345678",  // Same(1) 712345678 // Remove
"34456 18 787654321",  // Same(2) 787654321 // Remove
];

Expected out: //according to 8th string position to till end string char position check and need to remove duplicates.Because first five numbers are static then 6th and 7th position numbers are dynamic but no need to consider this for get unique. From 8th to till end position numbers only comparable with each other, based on it we have to get unique value.

[
"3445612712345678", 
"3445618787654321", 
];
RN Dev
  • 37
  • 5
  • Typescript does not change much there.. you could search exactly your question on stackoverflow.. or let me do that for you instead: [Get all unique values in a javascript array, remove duplicates](https://stackoverflow.com/questions/1960473/get-all-unique-values-in-a-javascript-array-remove-duplicates) – Peter Krebs Jun 14 '21 at 14:46
  • Does this answer your question? [Get all unique values in a JavaScript array (remove duplicates)](https://stackoverflow.com/questions/1960473/get-all-unique-values-in-a-javascript-array-remove-duplicates) – Peter Krebs Jun 14 '21 at 14:46
  • 1
    Can this return `[ "3445612712345678", "3445613787654321" ]` as well? Basically, the second element could be either `"34456 13 787654321"` or `"34456 18 787654321"`, right? – Eranga Heshan Jun 14 '21 at 14:47
  • @ErangaHeshan this is same for all index of values "34456" then second may vary so we cant compare it. Third is the main ("787654321") one which I need to compare and get unique values. – RN Dev Jun 14 '21 at 14:49
  • @ErangaHeshan Do you have any solution for this. Please help me on this. – RN Dev Jun 14 '21 at 16:08
  • 1
    @RNDev you haven't seemed to answer @ErangaHeshan's question. Your "duplicates" are actually different string values, so any solution will require picking *which* one of those values to keep. A simpler version of this is something like `["A1", "B2", "C1", "D2"]` and you want to have only one value for each unique number. So the output could be `["A1", "B2"]` or `["A1", "D2"]` or `["B2", "C1"]` or `["C1", "D2"]`. Which one is the right one? And even this assumes a filter operation that preserves original ordering (else `["C1", "B2"]` is possible). We need more information. – jcalz Jun 14 '21 at 17:51
  • @jcalz Actually in my array the values will be ["34456 12 712345678", "34456 13 787654321", "34456 15 712345678", "34456 18 787654321"]; If you seen here each values first five numbers are same in the array (34456). Then sixth and seventh numbers daynamic. So, I want to compare each and every array value eighth number to till end one to one and then remove duplicate then provide unique. First 7 values in each array string no need to consider but from 8th to till end we need to check and remove duplicates then provide final unique index. – RN Dev Jun 15 '21 at 17:02
  • Yes I understand all that but you have not explained how to tell which one to keep. I have asked the question as explicitly as I can. Is there a language barrier? – jcalz Jun 15 '21 at 17:25
  • @RNDev could you take a look at my answer? Let me know if this is what you are trying to achieve – axtck Jun 18 '21 at 12:41

1 Answers1

0

You can accomplish this by getting the unique values after slicing the last 9 characters. Then use find to find the first matches in the initial array.

const data = [
  "34456 12 712345678", // Same(1) 712345678 // Add
  "34456 13 787654321", // Same(2) 787654321 // Add
  "34456 15 712345678", // Same(1) 712345678 // Remove
  "34456 18 787654321", // Same(2) 787654321 // Remove
];

// get unique values (from last 9 characters)
const unique = [...new Set(data.map(item => item.slice(9)))];

const result = unique.map((u) => {
  return data.find((d) => d.includes(u)); // find matches
});

console.log(result);
axtck
  • 3,707
  • 2
  • 10
  • 26