# Distinct Values in an Array

Page 1 of 1

## 8 Replies - 32473 Views - Last Post: 11 November 2009 - 08:20 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=138385&amp;s=1f6a9154a778370c13ce7963d21dda11&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 bdavis

• New D.I.C Head

Reputation: 0
• Posts: 5
• Joined: 11-November 09

# Distinct Values in an Array

Posted 11 November 2009 - 12:59 PM

I have confused myself way to long with this simple program. I am supposed to write a program that reads 10 numbers and displays the distinct numbers. Example layout of the output is:

Enter an integer: 1
Enter an integer: 3
Enter an integer: 2
Enter an integer: 1
Enter an integer: 5
Enter an integer: 4
Enter an integer: 5
Enter an integer: 2
Enter an integer: 4
Enter an integer: 5
The number of distinct values is 5
1 3 2 5 4

I understand the logic. I even researched using a set, but this would be outside the assignment and I wouldnt probably get credit. I'm just missing how to link Array1 to Array2 and how does this run all in one loop. Any help would be appareciated it.

```import java.util.Scanner;

/**
*
* @author Bear
*/
public class Exercise6_5 {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);

String output = "";
String output1 = "";
int[] numbers = new int[10];
int DistinctArray[] = new int[numbers.length];
int count = 0;

for (int i = 0; i < numbers.length; i++) {
System.out.print("Enter a number:");
// Conver String into integer
numbers[i] = input.nextInt();

}

for (int i = 0; i < numbers.length; i++) {

int temp = numbers[i];
if (temp != numbers[i]) {

output += numbers[i] + " ";

}
else
DistinctArray[i] = temp - numbers[i];
output1 += DistinctArray[i] + " ";
count++;

System.out.println("The number of distinct values is: " + count);
System.out.println("The array is: " + output);
System.out.println("The test distinct array is: " + output1);

}
}
}

```

Is This A Good Question/Topic? 0

## Replies To: Distinct Values in an Array

### #2 sakshamkum

• D.I.C Head

Reputation: 19
• Posts: 232
• Joined: 09-June 09

## Re: Distinct Values in an Array

Posted 11 November 2009 - 01:06 PM

so can you clearly explain what the problem is
Was This Post Helpful? 0

### #3 Dogstopper

• The Ninjaducky

Reputation: 2965
• Posts: 11,222
• Joined: 15-July 08

## Re: Distinct Values in an Array

Posted 11 November 2009 - 01:26 PM

To do all of the distinct numbers in the loop, I would use a separate method, which I call isIn() to test whether or not an element is actually in the DistinctArray. If it is, then we simply add the number to the output list. If it is not yet in there, then we add it to the DistinctArray, increase the count, and add it to both strings. If you need just one loop for input and processing, I'm sure that would not require much at this point. I re-wrote your code with the above adjustments.

```import java.util.Scanner;

/**
*
* @author Bear
*/
public class Exercise6_5 {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);

int NUMELEMS = 10;

String output = "";
String output1 = "";
int[] numbers = new int[NUMELEMS];
int DistinctArray[] = new int[NUMELEMS];
int count = 0;

for (int i = 0; i < numbers.length; i++) {
System.out.print("Enter a number(Maximum of 10 numbers total): ");
// Conver String into integer
numbers[i] = input.nextInt();
}

for (int i = 0; i < numbers.length; i++) {

int temp = numbers[i];

// I would use a method call here
if (!isIn(temp, DistinctArray))
{
DistinctArray[i] = temp;
count++;
output += temp + " ";
output1 += temp + " ";
}
else
{
output += temp + " ";
}

}

System.out.println("The number of distinct values is: " + count);
System.out.println("The array is: " + output);
System.out.println("The test distinct array is: " + output1);
}

public static boolean isIn(int num, int[] a)
{
for (int i = 0; i < a.length; i++) {
if (a[i] == num)
return true;
}

return false;
}
}

```

Was This Post Helpful? 1

### #4 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12297
• Posts: 45,395
• Joined: 27-December 08

## Re: Distinct Values in an Array

Posted 11 November 2009 - 01:42 PM

You could use a HashSet<Integer> to solve your problem. A Set (HashSet implements the Set interface) is a data structure that holds only unique values. So I would just add all the numbers to the Set and then get the size() as the number of distinct values. So if you add the value 2 to the Set and it already exists, nothing will happen. Here's what I mean:

```HashSet<Integer> distinct = new HashSet<Integer>();
Scanner scan = new Scanner(System.in);
for(int i = 0; scan.hasNextInt() && i < 10; i++)
distinct.add(scan.nextInt());
System.out.println("Number of unique values " + distinct.size());

```

Also, like an ArrayList, Sets are resizable. In this way, you don't have to keep checking for unique values in your array. Good luck!
Was This Post Helpful? 1

### #5 Dogstopper

• The Ninjaducky

Reputation: 2965
• Posts: 11,222
• Joined: 15-July 08

## Re: Distinct Values in an Array

Posted 11 November 2009 - 01:44 PM

Quote

I understand the logic. I even researched using a set, but this would be outside the assignment and I wouldnt probably get credit.

That's why I didn't use a set. It still is nice to know though!
Thanks!

This post has been edited by Dogstopper: 11 November 2009 - 01:46 PM

Was This Post Helpful? 0

### #6 g00se

• D.I.C Lover

Reputation: 3540
• Posts: 16,130
• Joined: 20-September 08

## Re: Distinct Values in an Array

Posted 11 November 2009 - 01:55 PM

I didn't notice - were you given bounds on the actual values entered? I know that there should be maximum of ten values entered
Was This Post Helpful? 0

### #7 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12297
• Posts: 45,395
• Joined: 27-December 08

## Re: Distinct Values in an Array

Posted 11 November 2009 - 02:00 PM

Dogstopper, on 11 Nov, 2009 - 04:44 PM, said:

Quote

I understand the logic. I even researched using a set, but this would be outside the assignment and I wouldnt probably get credit.

That's why I didn't use a set. It still is nice to know though!
Thanks!

I completely missed that. Thanks for pointing that out.
Was This Post Helpful? 0

### #8 bdavis

• New D.I.C Head

Reputation: 0
• Posts: 5
• Joined: 11-November 09

## Re: Distinct Values in an Array

Posted 11 November 2009 - 08:05 PM

THank you for the help! Doggstopper I appreciate it. I knew i was close. Just had to interchange variables. Also the method statement was key. Thanks!
Was This Post Helpful? 0

### #9 Dogstopper

• The Ninjaducky

Reputation: 2965
• Posts: 11,222
• Joined: 15-July 08

## Re: Distinct Values in an Array

Posted 11 November 2009 - 08:20 PM

bdavis, on 11 Nov, 2009 - 09:05 PM, said:

THank you for the help! Doggstopper I appreciate it. I knew i was close. Just had to interchange variables. Also the method statement was key. Thanks!

You're very welcome! Good luck in future endeavors!

-- Cheers
Was This Post Helpful? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }