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
...
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
Copy link to clipboard
Copied
function keepUniques(/*str[]*/a, r,c,s,i,k)
{
for( r=a.slice(), c='\x01', s=c+a.join(c)+c, i=a.length ; i-- ;
k=c+a[i]+c, (s.indexOf(k)==s.lastIndexOf(k) || r.splice(i,1))
);
return r;
}
var arr = ["Apple", "Banana", "Cherry", "Apple", "Banana", "Dates", "Apple"];
alert( keepUniques(arr) ); // => Cherry,Dates
Copy link to clipboard
Copied
Hi @Marc Autret,
Few questions about your coding style.
I would wait to read your insights. Thanks for sharing your excellent knowledge with all of us.
-Manan
Copy link to clipboard
Copied
Hi @Manan Joshi
No, the purpose is not obfuscating, just sharing ideas and small routines in the most compact way. That's indeed a coding style I've cultivated over the years. Of course I still use semantic variable names in big functions, but they no longer make sense (to me) in generic algorithms. I exposed my thought process on this topic here:
Why I Do Not Use Meaningful Variable Names (Anymore)
Best,
Marc
Copy link to clipboard
Copied
That's quite a new way to look at things. Once I read the blog, it does not feel so out of place anymore. Infact, I feel inclined to experiment with it and see how it looks and feels. Of course, when working in a team I suspect everyone would like this thought and people would have a hard time understanding the code written by someone this way when they don't subscribe to this thought process. Thanks for sharing, I learn so much reading your posts, do write more often.
-Manan