Comparing two Arrays and remove duplicates from Original Array using JavaScript concat and slice

Advertisement
Post Pic

Introduction:

I would like to say this article going to help most of the developers who are in need to remove duplicates which are present in Original array by comparing with the other array elements. If you could not understand what I am saying, look at the below example you will get an idea on how we can play with JavaScript built-in functions.

Actual Task:

My actual task is, I have two arrays one naming Array1 and other naming Array2. In Array1 I have elements like “a”,”b”,”c”,”d”,”e”,”f” and in Array2 I have elements like “c”,”e” only. Now, I want to remove elements “c” and “e” from Array1 (by comparing Array2), hope you have understood now whats going to be the output yeah! the output should be Output: “a”,”b”,”d”,”f”. This can be achieved using concat and slice functions. Want, to know how we can achieve this, see below for respective code snippets:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<script language="javascript">
var Array1 = new Array("a","b","c","d","e","f");
var Array2 = new Array("c","e");
 
for (var i = 0; i<Array2.length; i++) {
	var arrlen = Array1.length;
	for (var j = 0; j<arrlen; j++) {
		if (Array2[i] == Array1[j]) {
			Array1 = Array1.slice(0, j).concat(Array1.slice(j+1, arrlen));
		}//if close
	}//for close
}//for close
alert(Array1);
</script>

Hope this might help some of the developers who are looking for the same logic. Please, let me know if you have any suggestions in making it more compatible enough by reducing lines of code.

Related Entries...

You Might Like:

Advertisement

Please let us know your feedback about the article through the comments section below, we want to hear from you about our website. And your comment / advice may help us to serve you better in upcoming articles.

Thank You!
Vivekanand [Founder of DeveloperSnippets]

7 Responses

[...] This post was Twitted by niceoutput – Real-url.org [...]

05.29.09

Nice snippet, Vivek! I know this is useful because Vivek utilized this concept on our team’s recent web application project.

Keep up the good work!

05.29.09

Hi Daniel,

Nice to see your comment here, thank you verymuch!

Thanks,
Vivek

05.29.09

woah! thanx a lot dude! it really helps!

05.29.09

Hi Joe,

Thanks a lot for your valuable comments, it really helps me in posting some new articles like this, which going to help everyone in all aspects. cheers… keep tunning into this site, its really going to help you a lot….

Thanks,
Vivek

05.29.09

Realy valuable code snipptes and smart too. Thanks for sharing.

05.29.09

hi there

and if there is an element in Array2 that is not present in array1??

how can i output ALSO array2 with the elements that dont match??
thx

Leave Your Response

* Name, Email, Comment are Required

Write For Us

Online Sponsors

Start Foreign Exchange Trading today with the Forex Affiliate Program and also you can have access Online Forex News to know what happens every single moment.


Forex Floor


Switch to our mobile site