Skip to content

Instantly share code, notes, and snippets.

@thesabbir
Last active April 5, 2023 16:32
Show Gist options
  • Save thesabbir/ad91620816a468e47efaf895bb2208b9 to your computer and use it in GitHub Desktop.
Save thesabbir/ad91620816a468e47efaf895bb2208b9 to your computer and use it in GitHub Desktop.
const input1 = [1, 1, 2, 2, 3, 3, 8, 4, 4];
const onlyOnce = (input) => {
const result = [];
input.filter((item) => {
let times = 0;
input.forEach((item2) => {
if (item === item2) {
times += 1;
}
});
if (times === 1) {
result.push(item);
}
});
return result[0];
};
console.log(onlyOnce(input1));
const input1 = "Hello world this is a test";
const k1 = 3;
const truncate = (input, k) => {
const words = input.split(" ");
return words.filter((word, index) => index <= k - 1).join(" ");
};
console.log(truncate(input1, k1));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment