Good morning everyone, I have the following problem, I have an array like the following:
["FI29","FI09","JM15","FI29"]
I am looking for some method or the way in which I could know if an element is repeated in my array, I would like a result something like this:
["FI29","FI09","JM15","FI29"]//devuelve true
["FI29","FI09","JM15","FI28"]//devuelve false
I was reading about the every()
and methods some()
, but I don't see how they can help me. Does anyone have an idea how to do it? The first thing that comes to mind is to go through the array in a nested way, so that it compares if there are repeated elements, but it seems very far-fetched to me. Anyway I put my idea here:
var array=["FI29","FI09","JM15","FI29"]//devuelve true
//["FI29","FI09","JM15","FI28"]//devuelve false
var repetido = false;
for (var i = 0; i < array.length; i++) {
for (var j = 0; j < array.length; j++) {
if (array[i] == array[j] && i != j) { //revisamos que i sea diferente de j, para que no compare el mismo elemento exacto.
repetido= true;
}
}
}
console.log(repetido);
A technique similar to the one proposed in this Stack Overflow answer to the question how to get the unique values of an array can be used.
the following function
will tell you if the element
valor
, which occupies the placeindice
inside thelista
given one, is the first time it appears in that list. The trick is based on the fact thatlista.indexOf(valor)
it returns the index of the first occurrence of the value. If it matches the parameterindice
, it is the first time it appears. If not, it is that we are facing a second, third, etc.Analogously you can define its opposite:
Having these functions, it is easy to do what you want. The list has no repeated elements as long as ALL of them (every) are the first time they appear. Therefore: it
lista.every(esPrimero)
will givetrue
in this case, andfalse
if it has repeated elements.If you want the opposite condition, the list will have repeated elements as long as SOME of them (some) is not the first time it appears. Therefore, it
lista.some(noEsPrimero)
will givetrue
in this case andfalse
if all the elements are different.show
It can be done very simply with a function that checks if any element of the array is repeated in the rest of it, asking from the subsequent position (position+1) of each element onwards.
For that you can use the function
some()
that checks if any element of the array meets a condition and the functionindexOf()
that indicates if a value exists in an array from one position onwards, leaving the code as follows:I consider this to be a more simplified and precise way than my original previous answer.
Previous Answer
To do this you can first use a function to remove the duplicate elements and then compare this new array with the original one:
I hope you find it useful, greetings.
It can be done using
new Set(array)
, this makes a new Set of elements (set of elements), which are not repeated.The number of elements in the set is obtained using
size
. Thesize
will always be less than or equal to the original amount of the array, so, to know if it has no repeats, you can compare that itnew Set(array)
is equal toarray.length
, but if you want to know if it has repeats, you check that it is different, as follows.Source: A comment in the SOes chat (by Pablo Lozano and lois6b)
Code:
I recently posted an answer but it has been deleted. I do not know why. Well, speaking of finding out repeated elements of an array, it's as simple as sorting the array and determining if the next and the previous one are the same. The operation is performed with a "for" loop.
I put a full page to see it. This is the code: