• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how can i do the same using javascript
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please someone help me with the code ??
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 15385
6
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks krishna for approach it did work


 
reply
    Bookmark Topic Watch Topic
  • New Topic