Remove same value in a array

Community Beginner ,
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.

Adobe Community Professional
Correct answer by Manan Joshi | Adobe Community Professional

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

 

TOPICS
Scripting

Views

114

Likes

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

Remove same value in a array

Community Beginner ,
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.

Adobe Community Professional
Correct answer by Manan Joshi | Adobe Community Professional

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

 

TOPICS
Scripting

Views

115

Likes

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
Nov 06, 2020 0
Adobe Community Professional ,
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

Likes

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
Reply
Loading...
Nov 06, 2020 0
Community Beginner ,
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

Likes

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
Reply
Loading...
Nov 06, 2020 0
Adobe Community Professional ,
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

Likes

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
Reply
Loading...
Nov 06, 2020 0
Community Beginner ,
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 

Likes

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
Reply
Loading...
Nov 06, 2020 0
Adobe Community Professional ,
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

Likes

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
Reply
Loading...
Nov 06, 2020 0
Community Beginner ,
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

Likes

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
Reply
Loading...
Nov 06, 2020 0
Adobe Community Professional ,
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

Likes

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
Reply
Loading...
Nov 06, 2020 0
Community Beginner ,
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

Likes

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
Reply
Loading...
Nov 06, 2020 0
Adobe Community Professional ,
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

 

Likes

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
Reply
Loading...
Nov 06, 2020 0
Community Beginner ,
Nov 06, 2020

Copy link to clipboard

Copied

Awesome Manan,

 

Thanks a lot, the lines working fine.

 

Thanks

Balaji

Likes

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
Reply
Loading...
Nov 06, 2020 0