473,396 Members | 1,707 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,396 software developers and data experts.

Compare 2 array

Hi..can anybody give coding for:

user input number of random number to generate.
then, add it to array.

the output like this:

array A = [ 1 2 2 3 4 5 6 2 2]

array B = [ 1 2 3 4 5 6 ]

*the repeat number in array A will not be display again in array B

now im still doing coding for it..
Oct 18 '06 #1
1 3491
r035198x
13,262 8TB
Hi..can anybody give coding for:

user input number of random number to generate.
then, add it to array.

the output like this:

array A = [ 1 2 2 3 4 5 6 2 2]

array B = [ 1 2 3 4 5 6 ]

*the repeat number in array A will not be display again in array B

now im still doing coding for it..
Here is a solution with an amazing number of for loops:

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. public class Duplicates {
  3.     public static void main(String[] args) {
  4.         BufferedReader inFile = null;
  5.  
  6.         try {
  7.             inFile = new BufferedReader(new InputStreamReader(System.in));
  8.             System.out.print("Enter number of numbers to be entered: ");
  9.             int a = Integer.parseInt(inFile.readLine());
  10.             System.out.println("Enter the numbers one by one");
  11.             int[] array = new int[a];
  12.             for(int i = 0; i < array.length; i++) {
  13.                 array[i] = Integer.parseInt(inFile.readLine());
  14.             }
  15.             int[] temp = new int[a];
  16.             int j = 0; int count = 0;
  17.             for(int i = 0; i < array.length; i++) {
  18.                 if(!Duplicates.in(temp, array[i])) {
  19.                     temp[j++] = array[i];
  20.                     count++;
  21.                 }
  22.             }
  23.             int[] noDup = new int[count];
  24.             for(int i = 0; i < count; i++) {
  25.                 noDup[i] = temp[i];
  26.             }
  27.  
  28.  
  29.             System.out.print("\nOriginally entered: ");
  30.             for(int i = 0; i < array.length; i++) {
  31.                 System.out.print(array[i] + " ");
  32.             }
  33.             System.out.print("\nWith duplicates removed: ");
  34.             for(int i = 0; i < noDup.length; i++) {
  35.                 System.out.print(noDup[i] + " ");
  36.             }
  37.  
  38.         }
  39.         catch(Exception e) {
  40.             e.printStackTrace();
  41.         }
  42.     }
  43.     public static boolean in(int[] a, int x) {
  44.         boolean isIn = false;
  45.         for(int i = 0; i < a.length; i++) {
  46.             if(a[i] == x) {
  47.                 return true;
  48.             }
  49.         }
  50.         return isIn;
  51.     }
  52.  
  53. }
Oct 19 '06 #2

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

Similar topics

0
by: Phil Powell | last post by:
/*-------------------------------------------------------------------------------------------------------------------------------- Parameters: $formField1: The name of the first array $formField2:...
5
by: Jim H | last post by:
I will have many 3 byte Byte arrays. Is there a way to compare the values without iterating through and comparing each element in the code? Example: byte lTest1 = new byte {0x1, 0x2, 0x3};...
5
by: sjoshi | last post by:
I have an SQLBinary (8) field(mts) with data like 0x01F4000000000000, 0x0400000000000000, etc. Now I need to compare just the first 4 bytes to an Int32 field (cts) and update the SQLBinary field if...
2
by: Tom | last post by:
What's the best way to compare two byte arrays? Right now I am converting them to base64 strings and comparing those, as so: 'result1 and result2 are two existing byte arrays that have been...
4
by: Justin Emlay | last post by:
I have two lists. These can be in either table form or array. That is, my data is in a dataset which I can move to an array if need be. ListA is a master list and it contains all items. ListB...
12
by: Assimalyst | last post by:
Hi, I have a working script that converts a dd/mm/yyyy text box date entry to yyyy/mm/dd and compares it to the current date, giving an error through an asp.net custom validator, it is as...
2
by: veinnz | last post by:
hi all... i'm kinda newbie in php...so i would like to ask ur guidance on how to compare the distribution of whitespace between two text area? so let's say text area 1 i type : hi how are you...
3
by: =?Utf-8?B?UmljY2FyZG8=?= | last post by:
What is the best method to compare 2 array to knw if each element is equal? Now, i have to compare the datarow.itemarray of 2 datarows wich is equals in structure. I tried to write this routine:...
3
by: raylopez99 | last post by:
This is an example of using multiple comparison criteria for IComparer/ Compare/CompareTo for List<and Array. Adapted from David Hayden's tutorial found on the net, but he used ArrayList so the...
4
by: toadstool | last post by:
I have an array containing the various products $inventory = array(); $inventory = "whatever1"; etc I have an array containing various values $quantity = array(100,200,300); I call in a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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...

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.