Unique Number Identifying


 
Thread Tools Search this Thread
Top Forums Programming Unique Number Identifying
# 1  
Old 07-23-2014
Unique Number Identifying

I'm trying to solve the below problem for a number:

Enter a number and if it has all unique digits print unique number else non-unique number.
Eg:
Code:
Input=123; Output=unique number
Input=112; Output=Non-unique number

The thing i tried is splitting the number into digits by using % operator and storing it in a array and i'm facing problems while using 2 for loops to traverse the array elements and compared with each other.

Code:
for ( i=0; i<=array_length; i++){
 for( j=i+1; j<=array_length; j++){
  if ( a[i] == a[j] ){
   printf("unique");
  }
 else{
   printf("Non-unique");
  }

I know that i'm wrong, can someone help with any idea to solve.
# 2  
Old 07-23-2014
for single input
Code:
 
echo "9284762" | awk '{t=$0; for(i=1; i<=length; i++) {if(a[t%10]++ > 0) {print $0 " has non-unique"; next}; t=sprintf("%d", t/10)}; print $0 " has uniques"}'

or if you want to pass multiple inputs through file
Code:
awk '{t=$0; for(i=1; i<=length; i++) {if(a[t%10]++ > 0) {print $0 " has non-unique"; next}; t=sprintf("%d", t/10)}; print $0 " has uniques"}' file

# 3  
Old 07-23-2014
If the digits are on the same line, you can use grep
Code:
if echo 123 | grep '\(.\).*\1' >/dev/null; then echo "Non-unique"; else echo "unique"; fi
if echo 112 | grep '\(.\).*\1' >/dev/null; then echo "Non-unique"; else echo "unique"; fi

\(.\) matches one character and saves a reference #1, .* means zero or more characters, \1 means that #1 re-occurs.
This User Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Identifying dupes within a database and creating unique sub-sets

Hello, I have a database of name variants with the following structure: variant=variant=variant The number of variants can be as many as thirty to forty. Since the database is quite large (at present around 60,000 lines) duplicate sets of variants creep in. Thus John=Johann=Jon and... (2 Replies)
Discussion started by: gimley
2 Replies

2. Shell Programming and Scripting

Concatenate lines with unique string AND number

In Bash using AWK or sed I need to convert the following file: ... numitem_tab0 =<p>1 KEYWORD</p><p>2 KEYWORD</p><p>3 KEYWORD</p><p>4 KEYWORD</p><p>5 KEYWORD</p>...<p>25 KEYWORD</p> subitem_tab0 =<p></p><p></p> ... numitem_tab6 =<p>1 KEYWORD</p><p>2 KEYWORD</p><p>3 KEYWORD</p><p>4 KEYWORD</p>... (2 Replies)
Discussion started by: pioavi
2 Replies

3. Shell Programming and Scripting

Finding the number of unique words in a file

find the number of unique words in a file using sort com- mand. (7 Replies)
Discussion started by: abhikamune
7 Replies

4. Shell Programming and Scripting

minimum number of unique key

input a 1 a 2 a -1 b 1 b 2 b 3 output a -1 b 1 Thanx ---------- Post updated at 09:42 PM ---------- Previous update was at 09:10 PM ---------- Ok I managed it (7 Replies)
Discussion started by: repinementer
7 Replies

5. Shell Programming and Scripting

Need to find Unique not used Number

Wrote a script to create a hidden account in OS X. It works perfect but I need to check if the UID is already in use before I tried to create the account. dscl . list /Users UniqueID | awk '{print $2}' | while read UIDS do if ; then echo "UID Is Already in USE" i=`expr "$2" - 1` echo... (4 Replies)
Discussion started by: elbombillo
4 Replies

6. Shell Programming and Scripting

display unique number

Hi, how i can display all the unique number from my random number script below; #!/usr/bin/perl use strict; my @alphanum = ( 'A' .. 'Z', 'a' .. 'z', 0 .. 9); my $random = join('', map($alphanum,(1..5))); print "$random\n"; Thank You. (1 Reply)
Discussion started by: malaysoul
1 Replies

7. Shell Programming and Scripting

Identifying the column number

I'd like to be able to identify in which column a string occurs. So far I know that I can tell how many columns there are and how to return a specific column: $ sar -r | grep 'kbswpcad' | awk 'NF = 9 { print $NF }' %swpused I've even managed to get the columns to output to an array but I... (2 Replies)
Discussion started by: pondlife
2 Replies

8. Shell Programming and Scripting

unique number for a date

Hello, In korn-shell, how can I do to have an unique number for a date done. I want to use it to have the number of days between two dates. Thanks in advance. (4 Replies)
Discussion started by: madmat
4 Replies

9. UNIX for Dummies Questions & Answers

identifying duplicates line & reporting their line number

I need to find to find duplicate lines in a document and then print the line numbers of the duplicates The files contain multiple lines with about 100 numbers on each line I need something that will output the line numbers where duplicates were found ie 1=5=7, 2=34=76 Any suggestions would be... (5 Replies)
Discussion started by: stresslog
5 Replies

10. UNIX for Dummies Questions & Answers

Directory Inode Number Not Unique

Hi, I know that inode for each file is unique, but is it the for the directory? So far I found different directories has the same inode nubmer when you do ls -i, could some one explain why? Thanks a lot. (9 Replies)
Discussion started by: nj302
9 Replies
Login or Register to Ask a Question