Copy link to clipboard
Copied
var indexArray = ["Apple", "Banana","Cherry","Apple","Banana","Dates"]
var uniquValues = unique(indexArray);
alert(uniquValues);
function unique(array) {
var o = {};
r = [];
for (i = 0; i < array.length; i++) {
o[array[i]] = array[i];
}
for (i in o) {
r.push(o[i]);
}
return r;
};
Hi team,
The output of the above coding is shown in below:
Output : Apple, Banana, Cherry, Dates
But i need to remove same values in indexArray and show output like as below:
Output: Cherry, Dates
please anyone give me suggestions.
Apologies, the variable d should have been an object, but I wrote it as an array. Try the following, it should work fine now hopefully. This code can be made more concise and more understandable, I am hopeful you would do that once it works for your use cases.
var indexArray =["Apple", "Banana","Cherry","Apple","Banana","Dates","Apple","Banana","Graphes"]
//var indexArray = ["Apple", "Banana","Cherry","Apple","Banana","Dates"]
var uniquValues = unique(indexArray);
alert(uniquValues);
function unique(array) {
var o = {};
r = [];
var d = {}
for (i = 0; i < array.length; i++) {
if(o[array[i]])
{
d[o[array[i]]] = 1
delete (o[array[i]])
}
else if(!d[array[i]])
o[array[i]] = array[i];
}
for (i in o) {
r.push(o[i]);
}
return r;
};
-Manan
Copy link to clipboard
Copied
var indexArray = ["Apple", "Banana","Cherry","Apple","Banana","Dates"]
var uniquValues = unique(indexArray);
alert(uniquValues);
function unique(array) {
var o = {};
r = [];
for (i = 0; i < array.length; i++) {
o[array[i]] = array[i];
}
for (i in o) {
r.push(o[i]);
}
return r;
};
Hi team,
The output of the above coding is shown in below:
Output : Apple, Banana, Cherry, Dates
But i need to remove same values in indexArray and show output like as below:
Output: Cherry, Dates
please anyone give me suggestions.
Apologies, the variable d should have been an object, but I wrote it as an array. Try the following, it should work fine now hopefully. This code can be made more concise and more understandable, I am hopeful you would do that once it works for your use cases.
var indexArray =["Apple", "Banana","Cherry","Apple","Banana","Dates","Apple","Banana","Graphes"]
//var indexArray = ["Apple", "Banana","Cherry","Apple","Banana","Dates"]
var uniquValues = unique(indexArray);
alert(uniquValues);
function unique(array) {
var o = {};
r = [];
var d = {}
for (i = 0; i < array.length; i++) {
if(o[array[i]])
{
d[o[array[i]]] = 1
delete (o[array[i]])
}
else if(!d[array[i]])
o[array[i]] = array[i];
}
for (i in o) {
r.push(o[i]);
}
return r;
};
-Manan
Copy link to clipboard
Copied
Try the following
var indexArray = ["Apple", "Banana","Cherry","Apple","Banana","Dates"]
var uniquValues = unique(indexArray);
alert(uniquValues);
function unique(array) {
var o = {};
r = [];
for (i = 0; i < array.length; i++) {
if(o[array[i]])
delete o[array[i]]
else
o[array[i]] = array[i];
}
for (i in o) {
r.push(o[i]);
}
return r;
};
-Manan
Copy link to clipboard
Copied
Hi Manan,
Superrrrrrr.
But,
Try this index value:
var indexArray = ["Apple", "Banana","Cherry","Apple","Banana","Dates","Apple"];
The index value must be removed, its available n numbers are OK but must be removed.
Thanks
Copy link to clipboard
Copied
I completely lost it, please explain again. I checked with the new value that you sent and the returned array has values Cherry, Dates, Apple which looks correct. What is the issue?
-Manan
Copy link to clipboard
Copied
Hi Manan,
The Duplicate values need to remove,
Output of your lines is: Cherry, Dates, Apple
but expected output is: Cherry, Dates
apple is duplicate value and beed to remove it come 2 or and above.
Thanks
Balaji
Copy link to clipboard
Copied
Try the following
var indexArray = ["Apple", "Banana","Cherry","Apple","Banana","Dates"]
var uniquValues = unique(indexArray);
alert(uniquValues);
function unique(array) {
var o = {};
r = [];
var d = []
for (i = 0; i < array.length; i++) {
if(o[array[i]])
{
d.push(o[array[i]])
delete (o[array[i]])
}
else if(!d[array[i]])
o[array[i]] = array[i];
}
for (i in o) {
r.push(o[i]);
}
return r;
};
-Manan
Copy link to clipboard
Copied
Hi Manan,
Thanks for support,
but still output shows: Cherry, Dates, Apple
Please ensure your lines.
Thanks
Balaji
Copy link to clipboard
Copied
Works for me just fine, see the screengrab of the same code executing on my machine
-Manan
Copy link to clipboard
Copied
Hi Manam,
Please use the following index value:
var indexArray = ["Apple", "Banana","Cherry","Apple","Banana","Dates","Apple","Banana","Graphes"]
Apple, Banana are repeated values. so need to remove those in the array
required output: Cherry, Dates and Graphes
Glad for your response every time.
Thanks
Copy link to clipboard
Copied
Apologies, the variable d should have been an object, but I wrote it as an array. Try the following, it should work fine now hopefully. This code can be made more concise and more understandable, I am hopeful you would do that once it works for your use cases.
var indexArray =["Apple", "Banana","Cherry","Apple","Banana","Dates","Apple","Banana","Graphes"]
//var indexArray = ["Apple", "Banana","Cherry","Apple","Banana","Dates"]
var uniquValues = unique(indexArray);
alert(uniquValues);
function unique(array) {
var o = {};
r = [];
var d = {}
for (i = 0; i < array.length; i++) {
if(o[array[i]])
{
d[o[array[i]]] = 1
delete (o[array[i]])
}
else if(!d[array[i]])
o[array[i]] = array[i];
}
for (i in o) {
r.push(o[i]);
}
return r;
};
-Manan
Copy link to clipboard
Copied
Awesome Manan,
Thanks a lot, the lines working fine.
Thanks
Balaji