Results 1 to 4 of 4
  1. #1
    hawaiifiver is offline Member
    Join Date
    Jan 2009
    Posts
    21
    Rep Power
    0

    Default Same values in an array

    Hello to all,

    I am wondering how to compare values in an array to see if any two values are the same.

    can i use nested for loops with this?

    Thanks.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by hawaiifiver View Post
    can i use nested for loops with this?
    That's the beauty of programming -- you have the ability and freedom to create something, and when doing this, you can try almost anything out. If it doesn't work, no harm, no foul.

    So my advice: sure give it a try, you've nothing to lose.

    Good luck.

  3. #3
    hawaiifiver is offline Member
    Join Date
    Jan 2009
    Posts
    21
    Rep Power
    0

    Default

    Hi all. I worked the following out. It works if there are two values. Like if i enter 2,3,3,4,5 or 5,6,7,8,6. But it wont work with 4,5,4,6,4. Any ideas?

    Java Code:
    class Array
    
    {
    
    	public static void main (String [] args)
    
    	{
    
    	int [] array = new int[5];
    	int count = 0;
    
    	System.out.println("Please enter five numbers:");
    
    	for (int i = 0; i < array.length; i ++)
    
    	{ array[i] = Console.readInt(); }
    
    	System.out.println();
    	System.out.println();
    
    
    
    
    
    
    	for (int i = 0; i < array.length; i ++)
    
    	{
    
    	for (int j = 1; j < array.length; j++)
    	{
    	if (array[i] == array[j] && j != i)
    	count++;
    
    	}
    
    
    	}
    
    	System.out.println();
    
    
    
    	System.out.println(count);
    
    	}// main
    
    }// class

  4. #4
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    its better to say:
    for (int j = i+1; j < array.length; j++)

    edit:
    and i would zero out the matched numbers. (or make it -1 or whatever)
    if (array[i] == array[j] && array[j] != 0)
    count++;
    array[j] = 0;
    }
    Last edited by angryboy; 02-24-2009 at 08:37 PM.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

Similar Threads

  1. 3d array filled with garbage values
    By jon80 in forum New To Java
    Replies: 5
    Last Post: 01-01-2009, 10:12 PM
  2. Array Assign Values from a Textfile
    By fawadafr in forum Java Applets
    Replies: 6
    Last Post: 11-30-2008, 12:10 AM
  3. writing array values to another class
    By ronald christian in forum New To Java
    Replies: 27
    Last Post: 11-07-2008, 04:08 PM
  4. replacing array values
    By Jononomous in forum New To Java
    Replies: 1
    Last Post: 05-22-2008, 03:27 PM
  5. [SOLVED] How to read a file and compare Array values
    By DonCash in forum Advanced Java
    Replies: 2
    Last Post: 04-02-2008, 02:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •