• Global community
    • Language:
      • Deutsch
      • English
      • EspaƱol
      • FranƧais
      • PortuguĆŖs
  • ę—„ęœ¬čŖžć‚³ćƒŸćƒ„ćƒ‹ćƒ†ć‚£
    Dedicated community for Japanese speakers
  • ķ•œźµ­ ģ»¤ė®¤ė‹ˆķ‹°
    Dedicated community for Korean speakers
Exit
0

Remove same value in a array

Participant ,
Nov 06, 2020 Nov 06, 2020

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.

TOPICS
Scripting

Views

519

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Nov 06, 2020 Nov 06, 2020

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
...

Votes

Translate

Translate
Community Expert ,
Nov 06, 2020 Nov 06, 2020

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Nov 06, 2020 Nov 06, 2020

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 06, 2020 Nov 06, 2020

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Nov 06, 2020 Nov 06, 2020

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 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 06, 2020 Nov 06, 2020

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Nov 06, 2020 Nov 06, 2020

Copy link to clipboard

Copied

Hi Manan,

 

Thanks for support, 

but still output shows: Cherry, Dates, Apple

 

Please ensure your lines.

 

Thanks

Balaji

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 06, 2020 Nov 06, 2020

Copy link to clipboard

Copied

Works for me just fine, see the screengrab of the same code executing on my machine

https://www.dropbox.com/s/t21op4f1srdlq9i/screen%20recording%202020-11-06%20at%206.50.02%20pm.mov?dl...

-Manan

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Nov 06, 2020 Nov 06, 2020

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 06, 2020 Nov 06, 2020

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

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Nov 06, 2020 Nov 06, 2020

Copy link to clipboard

Copied

Awesome Manan,

 

Thanks a lot, the lines working fine.

 

Thanks

Balaji

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Nov 06, 2020 Nov 06, 2020

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 07, 2020 Nov 07, 2020

Copy link to clipboard

Copied

Hi @Marc Autret,

Few questions about your coding style.

  • Thought process about making the local variables as functions params.
  • You variable naming choice. I have been following some of your code snippets and see that you mostly work with single-character variable names. Is it for obfuscating the code shared on the public platforms? If not, then how do you track what does a variable hold in big functions.
  • Your code is surely not the easiest to read and understand, at first sight, is the style deliberate again for obfuscation, or has it been cultivated over the years. What would you say is the benefit of this style if this is not obfuscation at play.

I would wait to read your insights. Thanks for sharing your excellent knowledge with all of us.

-Manan

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Nov 09, 2020 Nov 09, 2020

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 09, 2020 Nov 09, 2020

Copy link to clipboard

Copied

LATEST

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines