0

It should sort the array, remove the duplicate names from the array, and display the names in the right text area. Output: The list of alphabetic order names without any duplicated names.

function process() {
  var output = "";
  var inputlistNames = (document.getElementById('input').value);
  var list = inputlistNames.split('\n');
  var arr = inputlistNames.split('\n');
  var list = new Array(arr);
  list.sort();
  var listN = new Array(removeDuplicateUsingFilter(list));
  outputDiv = listN;
  document.getElementById('output').innerHTML = outputDiv;
}


function removeDuplicateUsingFilter(list) {
  var unique_array = [];
  for (var i = 0; i < list.length; i++) {
    if (unique_array.indexOf(list[i]) == -1) {
      unique_array.push(list[i])
    }
  }
  return unique_array;
}
<h1>list of names</h1>
<textarea id="input" rows="16" cols="20"></textarea>
<button type="button" onclick="process()">Remove Duplicates</button>
<textarea id="output" rows="16" cols="20"></textarea>

1
  • function removeDuplicateUsingFilter(list){ var unique_array = []; list.forEach(function(element) { if(!in_array(element, unique_array)){ unique_array.push(element); } }); return unique_array } Commented Jul 18, 2018 at 17:01

3 Answers 3

1

You could use a Map, which is type proof and prevents iterating again and again with Array#indexOf.

function removeDuplicateUsingFilter(list) {
   filtered = list.filter(function (a) {
     if (!this.has(a.toLowerCase())) {
        this.set(a, true);
        return true;
     }
   }, new Map);
  return filtered.sort()
}

a = ["genesis", "genesis", "ruben", "adeline", "ruben", "adeline", 
"fausto", "lauren", "zeke", "samantha", "Samantha", "Genesis"]

console.log(removeDuplicateUsingFilter(a))

//output: ["adeline", "fausto", "genesis", "lauren", "ruben", "samantha", "zeke"]
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you! but I've tried it doesn't sort in my code
Can you provide more context about what problems you're seeing?
I used your function but my programm doesn't sort alphabetic order names without any duplicated names.
Can you give me a sample input?
I updated my answer to provide sample input and output. I'm not sure exactly what behavior you're seeing on your end, but the code works as expected for me.
1

try this:

const words = [
  "genesis", 
  "genesis", 
  "ruben", 
  "adeline", 
  "ruben", 
  "adeline", 
  "fausto", 
  "lauren", 
  "zeke", 
  "samantha", 
  "Samantha", 
  "Genesis"
 ];

const sorted = Object.keys(words.reduce((res, word) => {
  return {...res, [word]:word}
}, {})).sort((a, b) => a.localeCompare(b));

console.log(sorted);

localCompare() function has many options to control sorting, and may handle cases like:

 const arr = [ "1", "10", "2" , "1A", "10a", "2a", "10A"];
 arr.sort((a,b)=>a.localeCompare(b, 'en', {numeric: true}));

Comments

0
list.filter(
    (elem) => {
         return list.find(elem) === -1 ? true : false;
    }
)
.sort(
    (a,b) => {
         return a > b ? 1 : -1;
    }
);

This should work

1 Comment

Your solution increases sorting complexity from O(n log n) to O(n ^ 2)

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.