473,416 Members | 1,663 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

My two arrays look like this:

4 Bit
Expand|Select|Wrap|Line Numbers
  1. $array1 = array(
  2. 'ADAIR',
  3. 'ADAM',
  4. 'ADAMINA',
  5. 'ADDISON',
  6. 'ADDY',
  7. 'ADELLE',
  8. 'ADEN',
  9. 'ADOLPH',
  10. 'ADRIANNA'
  11. );
  12.  
  13. $array2 = array(
  14. 'ADAIR',
  15. 'ADAMINA',
  16. 'ADRIANNA'
  17. );
How do I create a third array that is unique? Take the first array and eliminate duplicates from the second array.
Aug 18 '23 #1

✓ answered by Rina0

You can use the following Java code to create a third array that is unique by eliminating duplicates from the second array:
Expand|Select|Wrap|Line Numbers
  1. import java.util.Arrays;
  2. import java.util.HashSet;
  3.  
  4. public class RemoveDuplicates {
  5.  
  6.     public static void main(String[] args) {
  7.         String[] array1 = {"ADAIR", "ADAM", "ADAMINA", "ADDISON", "ADDY", "ADELLE", "ADEN", "ADOLPH", "ADRIANNA"};
  8.         String[] array2 = {"ADAIR", "ADAMINA", "ADRIANNA"};
  9.  
  10.         // Create a hash set to store the unique elements from the first array
  11.         HashSet<String> set = new HashSet<>(Arrays.asList(array1));
  12.  
  13.         // Create a new array to store the unique elements
  14.         String[] array3 = new String[set.size()];
  15.  
  16.         // Iterate over the hash set and add the elements to the new array
  17.         int i = 0;
  18.         for (String element : set) {
  19.             array3[i++] = element;
  20.         }
  21.  
  22.         // Print the unique elements
  23.         System.out.println(Arrays.toString(array3));
  24.     }
  25. }
  26.  
This code first creates a hash set to store the unique elements from the first array. Then, it creates a new array to store the unique elements. Finally, it iterates over the hash set and adds the elements to the new array. The following is the output of the code:
Expand|Select|Wrap|Line Numbers
  1. [ADAIR, ADAM, ADDISON, ADELLE, ADEN, ADRIANA, ADOLF]
As you can see, the third array only contains the unique elements from the first array. The duplicates from the second array have been removed.
Check out this blog post for reference.

1 13519
Rina0
13 Byte
You can use the following Java code to create a third array that is unique by eliminating duplicates from the second array:
Expand|Select|Wrap|Line Numbers
  1. import java.util.Arrays;
  2. import java.util.HashSet;
  3.  
  4. public class RemoveDuplicates {
  5.  
  6.     public static void main(String[] args) {
  7.         String[] array1 = {"ADAIR", "ADAM", "ADAMINA", "ADDISON", "ADDY", "ADELLE", "ADEN", "ADOLPH", "ADRIANNA"};
  8.         String[] array2 = {"ADAIR", "ADAMINA", "ADRIANNA"};
  9.  
  10.         // Create a hash set to store the unique elements from the first array
  11.         HashSet<String> set = new HashSet<>(Arrays.asList(array1));
  12.  
  13.         // Create a new array to store the unique elements
  14.         String[] array3 = new String[set.size()];
  15.  
  16.         // Iterate over the hash set and add the elements to the new array
  17.         int i = 0;
  18.         for (String element : set) {
  19.             array3[i++] = element;
  20.         }
  21.  
  22.         // Print the unique elements
  23.         System.out.println(Arrays.toString(array3));
  24.     }
  25. }
  26.  
This code first creates a hash set to store the unique elements from the first array. Then, it creates a new array to store the unique elements. Finally, it iterates over the hash set and adds the elements to the new array. The following is the output of the code:
Expand|Select|Wrap|Line Numbers
  1. [ADAIR, ADAM, ADDISON, ADELLE, ADEN, ADRIANA, ADOLF]
As you can see, the third array only contains the unique elements from the first array. The duplicates from the second array have been removed.
Check out this blog post for reference.
Aug 22 '23 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: 1111111111 | last post by:
Here is what I have so far... User is to enter 2 integers. One digit at a time. After that I have to print out what the user entered, and then add the two together. Example: Enter First Integer...
11
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to...
1
by: Nathan Gilbert | last post by:
I have a function that is returning a 2D array (declared using double pointers) and I want to be able to append a row of data to the beginning and end of this array without having to create a new...
5
by: al3x4nder | last post by:
look this code: #include <stdio.h> #define length(arr) sizeof(arr)/sizeof(arr) void func(int arr){ printf("func(): length(arr) = %d\n", length(arr)); } void main(void){
10
by: David Fort | last post by:
Hi, I'm upgrading a VB6 app to VB.net and I'm having a problem with a call to a function provided in a DLL. The function takes the address of a structure which it will fill in with values. I...
2
by: Eric Lilja | last post by:
...during the call to swap_arrays the name of the array decays to a pointer to its first element and that pointer is a temporary variable and you cant bind a non-const reference to a temporary....
14
by: Tobiah | last post by:
, , , , ] -- Posted via a free Usenet account from http://www.teranews.com
127
by: sanjay.vasudevan | last post by:
Why are the following declarations invalid in C? int f(); int f(); It would be great if anyone could also explain the design decision for such a language restricton. Regards, Sanjay
2
by: ratinox | last post by:
This is the code i was working on. i know it is clunky but im new to this. at the spot that sais RIGHT HERE i am getting this error statement when i try to run it: when casting from a number the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.