forked from Oluwasetemi/altschool-opensource-names
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testDuplicate.js
36 lines (25 loc) · 829 Bytes
/
testDuplicate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*
This file loops through the names in names.txt and prints
duplicates
*/
const fs = require("fs");
const hasDuplicates = (arr) => arr.length !== new Set(arr).size;
const findDuplicates = (list) => {
list.sort(); // alters original array
let ans = [];
for (let i = 0; i < list.length; i++) {
if (list[i] === list[i + 1]) {
if (ans[ans.length - 1] !== list[i]) {
ans.push(list[i]);
}
}
}
return ans;
};
const contentString = fs.readFileSync("./names.txt", { encoding: "utf-8" });
const contentArray = contentString.split("\n");
const contentArrayLength = contentArray.length;
let hasDuplicate = hasDuplicates(contentArray);
let hasDuplicateArray = findDuplicates(contentArray);
console.log(hasDuplicateArray)
console.log(hasDuplicate ? "Duplicates exist" : "No duplicate found");