|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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. |
|
#2
|
||||
|
||||
|
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 |
|
#3
|
|||
|
|||
|
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 |
|
#4
|
||||
|
||||
|
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. |
|
#5
|
|||
|
|||
|
<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 |
|
#6
|
||||
|
||||
|
i think it works dog!
![]() |
|
#7
|
|||
|
|||
|
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> |
|
#8
|
||||
|
||||
|
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; |
|
#9
|
|||
|
|||
|
Code:
numlist = [1,2,3,3,4,3,4,5,]
unique = []
for item in numlist:
if item not in unique: unique.append(item)
|
|
#10
|
|||
|
|||
|
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 |
|
#11
|
|||
|
|||
|
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.)
|
|
#12
|
|||
|
|||
|
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; } |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > Software Design > Logic to get the unique numbers from an array |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|