I'm trying to do something interesting in JavaScript, but I can't. This is my input:
var Input = ['a','a','a','b','b','b','b','c','c','c','a','a','c','d','d','d'];
So my output is that only get differents values and go in a new vector.
var Output = SomeFunction(Input);
This is what I want:
Output = ['a','b','c','a','c','d'];
Y tried with this, but don't work aswell:
function SomeFunction(input){
var out= [];
for (var i = 0; i < input.length - 1; i++) {
if(input[i] == input[i+1]){
out.push(input[i]);
}
}
return out;
}
I'm trying to do something interesting in JavaScript, but I can't. This is my input:
var Input = ['a','a','a','b','b','b','b','c','c','c','a','a','c','d','d','d'];
So my output is that only get differents values and go in a new vector.
var Output = SomeFunction(Input);
This is what I want:
Output = ['a','b','c','a','c','d'];
Y tried with this, but don't work aswell:
function SomeFunction(input){
var out= [];
for (var i = 0; i < input.length - 1; i++) {
if(input[i] == input[i+1]){
out.push(input[i]);
}
}
return out;
}
Share
Improve this question
edited May 29, 2018 at 9:30
Cœur
38.8k26 gold badges205 silver badges277 bronze badges
asked Oct 14, 2015 at 8:45
Alberto AcuñaAlberto Acuña
5453 gold badges10 silver badges30 bronze badges
4
-
1
I think it should be
if(input[i] != input[i+1])
and check the last element separately. – Passerby Commented Oct 14, 2015 at 8:47 - 1 If you only want the unique values you can try this stackoverflow./questions/1960473/unique-values-in-an-array – Yvo Cilon Commented Oct 14, 2015 at 8:47
- Is the input an array or a string, and you've converted the string to array? – Tushar Commented Oct 14, 2015 at 8:55
-
I'd loop over
input
with a for loop, and keep an indexprev
pointing to the index ininput
of the value most recently added toout
(starting with the first value). Then only add a new value and advanceprev
to the current index the value at the current index is different than the value at indexprev
. – aaroncarsonart Commented Oct 14, 2015 at 8:55
9 Answers
Reset to default 5You can use filter()
var input = ['a', 'a', 'a', 'b', 'b', 'b', 'b', 'c', 'c', 'c', 'a', 'a', 'c', 'd', 'd', 'd'];
input = input.filter(function(v, i, arr) {
return arr[i - 1] !== v;
//pare with the previous value
})
document.write(JSON.stringify(input));
You can use backreferencing regex
- Convert the array to string by using
join
, so that regex can be used on it - Use backreferencing regex to remove the consecutive elements with replace()
- Convert back the string to array using
split
(\w)\1*
Explanation
var input = ['a', 'a', 'a', 'b', 'b', 'b', 'b', 'c', 'c', 'c', 'a', 'a', 'c', 'd', 'd', 'd'];
var str = input.join('');
input = str.replace(/(\w)\1*/g, '$1').split('');
console.log(input);
document.write('<pre>' + JSON.stringify(input, 0, 2) + '</pre>');
You can do a filter like
var Input = ['a', 'a', 'a', 'b', 'b', 'b', 'b', 'c', 'c', 'c', 'a', 'a', 'c', 'd', 'd', 'd', 'e'];
var Output = SomeFunction(Input);
function SomeFunction(input) {
var out = input.filter(function(value, i) {
return value !== input[i + 1]
});
return out;
}
output.innerHTML = JSON.stringify(Output)
<pre id="output"><pre>
Try like this
var out= [];
var i = 0;
for (i = 0; i < input.length - 1; i++) {
if(input[i] != input[i+1]){
out.push(input[i]);
}
}
if (out[out.length-1] !== input[i])
out.push(input[i]);
How about this:
var Input = ['a', 'a', 'a', 'b', 'b', 'b', 'b', 'c', 'c', 'c', 'a', 'a', 'c', 'd', 'd', 'd'];
function SomeFunction(input) {
var out = [];
var initStr = input[0];
console.log(initStr)
for (var i = 1; i < input.length; i++) {
if (input[i] === initStr) {
} else {
out.push(input[i - 1]);
initStr = input[i];
}
}
out.push(input[i - 1]);
console.log(out);
}
SomeFunction(Input)
function SomeFunction(input) {
var out= [];
out.push(input[i]);
for (var i = 0; i < input.length - 1; i++) {
if(input[i] !== out[out.length-1]){
out.push(input[i]);
}
}
return out;
}
Worked for me:
function SomeFunction(input){
var out= [];
for (var i = 0; i < input.length; i++) {
if(input[i] !== input[i+1]){
out.push(input[i]);
}
}
return out;
}
Be careful with the name of the varible "input". It's not "Input", it's "input".
Example using array filters.
Pretty simple as long as the array is not too large and could rather easily be extended to pare some property in an array of objects. With a larger array it might be faster though to do a for loop instead as in some of the other answers.
var Input = ['a', 'b', 'c','a', 'b', 'a', 'b', 'c','c']
var Output = Input.filter(function(value,index) { return Input[index - 1] != value; });
var Input = ['a','a','a','b','b','b','b','c','c','c','a','a','c','d','d','d'];
var Output = SomeFunction(Input);
function SomeFunction(input){
var out= [];
for (var i = 1; i < input.length; i++) {
if(input[i] != input[i-1]){
out.push(input[i-1]);
}
}
out.push(input[input.length - 1]);
return out;
}
alert(Output);