0% found this document useful (0 votes)
18 views

Public Class Public Static Void: "Not Unique"

The document contains code for two Java methods - isUnique() and isUniqueArray(). The isUnique() method takes a string as a parameter and returns "is unique" if all characters are unique or "not unique" if duplicates are found. The isUniqueArray() method checks if a string's characters are unique and returns a boolean.

Uploaded by

dd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Public Class Public Static Void: "Not Unique"

The document contains code for two Java methods - isUnique() and isUniqueArray(). The isUnique() method takes a string as a parameter and returns "is unique" if all characters are unique or "not unique" if duplicates are found. The isUniqueArray() method checks if a string's characters are unique and returns a boolean.

Uploaded by

dd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

File - C:\Users\daria\OneDrive\Programming\GitHub\Java\JavaPractice\src\UniqueChar.

java
1 public class UniqueChar {
2
3     public static void main(String[] args) {
4 //        String str = "hey";
5 //        System.out.println(isUnique(str));
6
7
8 //        String[] words = {"abcde", "hello", "apple", "kite", "
padle"};
9 //        for (String word:words) {
10 //            System.out.println(word+": "+isUniqueArray(word));
11 //        }
12
13     }
14
15     public static String isUnique(String str) {
16         for (int i=0; i<=str.length()‐1; i++){
17             for (int j=i+1; j<=str.length()‐1; j++){
18                 if (str.charAt(i)==str.charAt(j)){
19                     return "not unique";
20                 }
21             }
22         }
23         return "is unique";
24     }
25
26     public static boolean isUniqueArray(String str){
27         if (str.length()>128){
28             return false;
29         }
30
31         for (int i=0; i<=str.length()‐1; i++){
32             for (int j=i+1; j<=str.length()‐1; j++){
33                 if (str.charAt(i)==str.charAt(j)){
34                     return false;
35                 }
36             }
37         }
38         return true;
39     }
40
41
42 }
43

Page 1 of 1

You might also like