- $array1 = array(
-
'ADAIR',
-
'ADAM',
-
'ADAMINA',
-
'ADDISON',
-
'ADDY',
-
'ADELLE',
-
'ADEN',
-
'ADOLPH',
-
'ADRIANNA'
-
);
-
-
$array2 = array(
-
'ADAIR',
-
'ADAMINA',
-
'ADRIANNA'
-
);
How do I create a third array that is unique? Take the first array and eliminate duplicates from the second array.
You can use the following Java code to create a third array that is unique by eliminating duplicates from the second array: - import java.util.Arrays;
-
import java.util.HashSet;
-
-
public class RemoveDuplicates {
-
-
public static void main(String[] args) {
-
String[] array1 = {"ADAIR", "ADAM", "ADAMINA", "ADDISON", "ADDY", "ADELLE", "ADEN", "ADOLPH", "ADRIANNA"};
-
String[] array2 = {"ADAIR", "ADAMINA", "ADRIANNA"};
-
-
// Create a hash set to store the unique elements from the first array
-
HashSet<String> set = new HashSet<>(Arrays.asList(array1));
-
-
// Create a new array to store the unique elements
-
String[] array3 = new String[set.size()];
-
-
// Iterate over the hash set and add the elements to the new array
-
int i = 0;
-
for (String element : set) {
-
array3[i++] = element;
-
}
-
-
// Print the unique elements
-
System.out.println(Arrays.toString(array3));
-
}
-
}
-
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: - [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
You can use the following Java code to create a third array that is unique by eliminating duplicates from the second array: - import java.util.Arrays;
-
import java.util.HashSet;
-
-
public class RemoveDuplicates {
-
-
public static void main(String[] args) {
-
String[] array1 = {"ADAIR", "ADAM", "ADAMINA", "ADDISON", "ADDY", "ADELLE", "ADEN", "ADOLPH", "ADRIANNA"};
-
String[] array2 = {"ADAIR", "ADAMINA", "ADRIANNA"};
-
-
// Create a hash set to store the unique elements from the first array
-
HashSet<String> set = new HashSet<>(Arrays.asList(array1));
-
-
// Create a new array to store the unique elements
-
String[] array3 = new String[set.size()];
-
-
// Iterate over the hash set and add the elements to the new array
-
int i = 0;
-
for (String element : set) {
-
array3[i++] = element;
-
}
-
-
// Print the unique elements
-
System.out.println(Arrays.toString(array3));
-
}
-
}
-
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: - [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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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){
|
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...
|
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....
|
by: Tobiah |
last post by:
, , , , ]
--
Posted via a free Usenet account from http://www.teranews.com
|
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
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
|
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...
|
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...
|
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,...
|
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...
| |