admin管理员组

文章数量:1300226

How can I get one unique value in an array or string? Only the first value. Pure JS only.

My example:

function searchValue () {
  let inputText = [1,1,4,2,2,2,3,1];
  let foundedValue;
  for (let i = 0; i < inputText.length; i++) {
    if (i === inputText.indexOf(inputText[i]) && i === inputText.lastIndexOf(inputText[i])) {
      foundedValue = inputText[i];
      break;
    } else {
      foundedValue = "not founded.";
    }
  }
  return foundedValue;
}
console.log("Search value: "+ searchValue())

Answer is 4.

But, I need a short solution. Using the find() and filter() functions.

How can I get one unique value in an array or string? Only the first value. Pure JS only.

My example:

function searchValue () {
  let inputText = [1,1,4,2,2,2,3,1];
  let foundedValue;
  for (let i = 0; i < inputText.length; i++) {
    if (i === inputText.indexOf(inputText[i]) && i === inputText.lastIndexOf(inputText[i])) {
      foundedValue = inputText[i];
      break;
    } else {
      foundedValue = "not founded.";
    }
  }
  return foundedValue;
}
console.log("Search value: "+ searchValue())

Answer is 4.

But, I need a short solution. Using the find() and filter() functions.

Share Improve this question edited Dec 21, 2018 at 17:58 benvc 15.1k4 gold badges38 silver badges57 bronze badges asked Oct 31, 2018 at 20:39 RednaksiRednaksi 311 silver badge2 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 5

You can find the first unique item in your array using find() and paring indexOf() to lastIndexOf() to determine whether or not there is more than one instance of an item in the array. If you need to find unique characters in a string, then you can first split it into an array and then use the same approach.

const arr = [1, 1, 4, 2, 2, 2, 3, 1];
const result = arr.find((x) => arr.indexOf(x) === arr.lastIndexOf(x));
console.log(result);
// 4

const text = 'aadbbbca';
const textarr = text.split('');
const textresult = textarr.find((x) => textarr.indexOf(x) === textarr.lastIndexOf(x));
console.log(textresult);
// d

You can try this.

const arr = [1, 1, 4, 2, 2, 2, 3, 1];
let r = {};
arr.map(a => r[a] = (r[a] || 0) +1)
var res = arr.find(a => r[a] === 1 )
console.log(res)

You can use js Set() object. At first you could create a Set of duplicated elements.

const inputText = [1,1,4,2,2,2,3,1];

const duplicatesSet= inputText.reduce((dupSet, el) => 
    inputText.filter(arrEl => arrEl === el).length > 1 ?
      dupSet.add(el) : dupSet
, new Set());

Second you could use array.find. It returns first duplicated element.

const firstDupElement = inputText.find(el => duplicatesSet.has(el));
const searchValue = (_param) => {
  for (let i= 0; i < _param.length; i+= 1) {
    if (_param.indexOf(_param[i]) === _param.lastIndexOf(_param[i])) return _param[i];
  }
  return "not founded.";
}

let arr = [1,1,2,2,2,1,3,1,4,4,5]

const dupelearray  = (array) => {
let arr2 =[...arr]
let ele = []
let state = false
    arr2.map((i,index)=>{
        arr2.splice(index,1)
    arr.map((j)=>{
     return arr2.includes(j) ? null : state=true
})
state && ele.push(i)
state=false
arr2.splice(index,0,i)
})
return console.log(arr.indexOf(ele[0]))
}
dupelearray(arr)

wow i didnt knew lastindexof method and was making this algo so difficult btw this solution also works but definitely i am new in algo so this will take much more time but the solution still works!!!!!! damn should remember more methods or you have to think so much -_-

本文标签: javascriptFind the first unique value in an array or stringStack Overflow