Software Design
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreSoftware Design

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 7th, 2003, 06:43 AM
getbabs getbabs is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 4 getbabs User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Logic to get the unique numbers from an array

Hi All,

Pls help me with a logic to get the unique numbers from an array.

For Ex:

if arr[] contains 20,30,40,20,30
then arr1[] should contain 20,30,40.

Reply With Quote
  #2  
Old July 7th, 2003, 09:15 AM
heinrich's Avatar
heinrich heinrich is offline
digital sinner
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Location: sinner's land
Posts: 68 heinrich User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 37 m 30 sec
Reputation Power: 6
Send a message via Yahoo to heinrich
Code:
for (i=1;i<=array.length;i++) {
    found=false;
    for (k=i+1;k<=array.length;k++) {
        if (array[i]==array[k]) found=true;
        }
    if (!found) println(array[i]);
    }

hope it works

Reply With Quote
  #3  
Old July 8th, 2003, 05:20 AM
mathurnitin mathurnitin is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Location: Bangalore, India
Posts: 13 mathurnitin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 30 m 10 sec
Reputation Power: 0
Send a message via Yahoo to mathurnitin
Try searching through the array to find a number exists or not. Searching can be done in numerous ways:
linear search, binary search etc......

Best Regards
Nitin Mathur

Reply With Quote
  #4  
Old July 9th, 2003, 04:34 PM
dog135's Avatar
dog135 dog135 is offline
Doggie
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jul 2003
Location: Seattle, WA
Posts: 751 dog135 User rank is Corporal (100 - 500 Reputation Level)dog135 User rank is Corporal (100 - 500 Reputation Level)dog135 User rank is Corporal (100 - 500 Reputation Level)dog135 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 10 h 38 m 25 sec
Reputation Power: 7
heinrich, your code would only return "40" from that list.

Are the range of values limited? For instance, if the range of numbers is 0-255, you could create a 256 element array, with each element set to "0", then set each element to "1" as you come across it. Then display all the "1"s as a list. If using a language that allows sparce arrays, this would be most effective. (I've used this in both Perl and Mumps)

Otherwise, you'd probably want to create a second list, and as you read in each value, check to see if it's on the second list, if it is, skip it, otherwise add it to the list. Then print the second list out.

Reply With Quote
  #5  
Old July 10th, 2003, 11:27 AM
SlankenOgen SlankenOgen is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Location: The Netherlands
Posts: 122 SlankenOgen User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
<script language = "javascript">
var arr = new Array(1,1,2,3,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6,7,7,7);
var newArray = new Array();
arr.sort();

newArray[0] = arr[0];
var lgn = arr.length;

var j = 0;
var t = 0;
var temp = 0;

function A(){

for(j = 0; j < lgn; j++){

temp = arr[j];
if(temp == arr[j+1]){
continue;
}

newArray[t++] = temp;

}

alert(newArray);
}
</script>
__________________
~mgb

Reply With Quote
  #6  
Old July 10th, 2003, 02:28 PM
heinrich's Avatar
heinrich heinrich is offline
digital sinner
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Location: sinner's land
Posts: 68 heinrich User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 37 m 30 sec
Reputation Power: 6
Send a message via Yahoo to heinrich
i think it works dog!

Reply With Quote
  #7  
Old July 10th, 2003, 04:26 PM
SlankenOgen SlankenOgen is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Location: The Netherlands
Posts: 122 SlankenOgen User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
A slightly cleaner version-

<script language = "javascript">

var arr = new Array(1,1,2,3,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6,7,7,7);

var newArray = new Array();

arr.sort();

var lgn = arr.length;

var j = 1;
var t = 0;
var temp = 0;

function A(){

for(j = 1; j <= lgn; j++){

temp = arr[j-1];
if(temp == arr[j%lgn]){
continue;
}

newArray[t++] = temp;

}

alert(newArray);
}
</script>

Reply With Quote
  #8  
Old July 10th, 2003, 05:24 PM
icrf's Avatar
icrf icrf is offline
Perl Monkey
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: May 2003
Location: the far end of town where the Grickle-grass grows
Posts: 1,856 icrf User rank is Second Lieutenant (5000 - 10000 Reputation Level)icrf User rank is Second Lieutenant (5000 - 10000 Reputation Level)icrf User rank is Second Lieutenant (5000 - 10000 Reputation Level)icrf User rank is Second Lieutenant (5000 - 10000 Reputation Level)icrf User rank is Second Lieutenant (5000 - 10000 Reputation Level)icrf User rank is Second Lieutenant (5000 - 10000 Reputation Level)icrf User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 4 Days 10 h 28 m 31 sec
Reputation Power: 103
Send a message via AIM to icrf
The simplest way would be a function with a pair of nested for loops that takes an array and a value returns a boolean value if it finds that value in the array. Initialize a unique array to empty. Loop over each value in your given array, calling that function to see if each value is in your unique array or not. The ones that aren't already get pushed on it.

But it depends on your language. That method should work for any language. An efficient way would be to use a good sort and then a binary search for each element. This would be especially easy if the language has sorting routines built in for you (like the javascript above).

Another efficent method is using hash tables, easiest if the language supports them, too. The simple Perl solution:

my @array = (1,1,2,3,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6,7,7,7);
my %hash = map { $_ => 1 } @array;
print sort keys %hash;

Reply With Quote
  #9  
Old July 10th, 2003, 07:47 PM
inkedmn inkedmn is offline
Tattooed Python-Lovin' Freak-Boy
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2001
Location: orange county, CA
Posts: 16 inkedmn User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to inkedmn Send a message via AIM to inkedmn
Code:
numlist = [1,2,3,3,4,3,4,5,]
unique = []
for item in numlist:
    if item not in unique: unique.append(item)

Reply With Quote
  #10  
Old September 30th, 2008, 08:20 PM
krisnil krisnil is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2008
Posts: 5 krisnil User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 24 m 51 sec
Reputation Power: 0
Counting Unique Numbers

i have a similar problem, does anyone know how we can count the number of unique numbers in a array

example x=[1,2,2,2,5,6]
y=[1,2,3,4,5,6,8]

output is '7'
because 1,2,3,4,5,6,8 are unique

Reply With Quote
  #11  
Old September 30th, 2008, 08:59 PM
Lux Perpetua Lux Perpetua is online now
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2004
Location: San Francisco Bay
Posts: 1,475 Lux Perpetua User rank is Major (30000 - 40000 Reputation Level)Lux Perpetua User rank is Major (30000 - 40000 Reputation Level)Lux Perpetua User rank is Major (30000 - 40000 Reputation Level)Lux Perpetua User rank is Major (30000 - 40000 Reputation Level)Lux Perpetua User rank is Major (30000 - 40000 Reputation Level)Lux Perpetua User rank is Major (30000 - 40000 Reputation Level)Lux Perpetua User rank is Major (30000 - 40000 Reputation Level)Lux Perpetua User rank is Major (30000 - 40000 Reputation Level)Lux Perpetua User rank is Major (30000 - 40000 Reputation Level)Lux Perpetua User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 3 Weeks 6 Days 20 m 57 sec
Reputation Power: 377
Sort the array and then treat each contiguous range of equal elements as a single distinct element. (Sorting solves most obvious problems related to element distinctness.)

Reply With Quote
  #12  
Old October 1st, 2008, 02:51 AM
krisnil krisnil is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2008
Posts: 5 krisnil User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 24 m 51 sec
Reputation Power: 0
palindrome

hey guyz I have this exercise to do a palindrome.
However Iam having trouble running it.
Any advice?

code:
import java.io.*;


public class Singles{

public static void main (String[] args){


//Here it ignores the case of the letters and determines if it is a Palindrome

public static boolean isPalindrome (String word);
if (word.equalsIgnoreCase(reverse(word))) {
return true;
} else {
return false;
}
}

//This is the part the performs the task
//word.equalsIgnoreCase(reverse(word));


public static String reverse (String in) {
String out = "";
for (int i = 0; i < in.length(); i++) {
out += String.valueOf(in.charAt((in.length() - 1) - i));
}
return out;
}

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreSoftware Design > Logic to get the unique numbers from an array


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |