Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Robby Jain
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Robby Jain
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how can i do the same using javascript
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it would be nice of you to explain me with a programming example since i am totally new to javascript
 
Robby Jain
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please someone help me with the code ??
 
vamsi krishna Rajulapati
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15385
6
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks krishna for approach it did work


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic