aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes program to copy one array into another such that repetation is not allowed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "program to copy one array into another such that repetation is not allowed" Watch "program to copy one array into another such that repetation is not allowed" New topic
Author

program to copy one array into another such that repetation is not allowed

Robby Jain
Greenhorn

Joined: Apr 30, 2012
Posts: 9
How to write a program in java to copy one array into another array such the elements which are repeated gets copied only once in the other arrray
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

One way would be to add the contents of the array into a Set, then use the toArray() method to get it back out, now without duplicates.

Another way would be to create a second Array as large as the first array, take each value out of the source, loop over the values in the second and if the values doesn't already exist put it in the second array and increment a counter. Then create a third array using the counter to determine its size and copy values from the second array into the third. This process could be made faster if the values are sortable by first sorting the source array (for the least number of comparisons).

Steve
Robby Jain
Greenhorn

Joined: Apr 30, 2012
Posts: 9
how can i do the same using javascript
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
I would suggest a combination of those two methods. Use a second array, and add each element if it has not already been added. Use the set to maintain those already added. That will run in linear time, whilst using an array on its own runs in quadratic time. Using a Set in tandem with a List (maybe this) will probably allow you to do the whole thing in linear time, and maintain ordering, too.
Robby Jain
Greenhorn

Joined: Apr 30, 2012
Posts: 9
it would be nice of you to explain me with a programming example since i am totally new to javascript
Robby Jain
Greenhorn

Joined: Apr 30, 2012
Posts: 9
please someone help me with the code ??
vamsi krishna Rajulapati
Greenhorn

Joined: Apr 25, 2012
Posts: 2
we can make it in a easy way.

First take one empty array. Now we need add the elements in first array to second array .Now take for loop and inside the for loop, write 'if' condition. In each iteration we need to add the elements into empty/second array. Before adding the element we need to check the element existence in the second array i.e in which we are adding elements.
If the element exists then we do nothing, else we add the element to the second array.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Robby Jain wrote:How to write a program in java to copy one array into another array such the elements which are repeated gets copied only once in the other arrray


What have YOU tried, we are not here to do your homework.

Eric
Robby Jain
Greenhorn

Joined: Apr 30, 2012
Posts: 9
Thanks krishna for approach it did work


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: program to copy one array into another such that repetation is not allowed