-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path58. Get unique characters from string .js
More file actions
96 lines (79 loc) · 2.88 KB
/
58. Get unique characters from string .js
File metadata and controls
96 lines (79 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// get unique characters from string :
// --------------------------------------------------------------------------------------------------------------::
// Add characters only if not already present
const text = "programming";
let uniqueChars = ""; // Result string with unique characters
// Go through each character in the original string
for (let i = 0; i < text.length; i++) {
let currentChar = text[i]; // Get current character
let alreadyExists = false; // Flag to check if character exists
// Check if this character is already in our result
for (let j = 0; j < uniqueChars.length; j++) {
if (uniqueChars[j] === currentChar) {
alreadyExists = true; // Found duplicate
break; // Stop checking, we found it
}
}
// Add character only if it's not already there
if (alreadyExists === false) {
uniqueChars = uniqueChars + currentChar; // Add to result
}
}
console.log(uniqueChars);
/*
APPROACH:
"I'll check each character and add it only if it's not already in my result"
ALGORITHM STEPS:
1. Go through each character in original string
2. Check if that character is already in result string
3. If not found, add it to result
4. If found, skip it (it's a duplicate)
5. Final result has each character only once
EXAMPLE WITH "programming":
- p: not in result → add → result: "p"
- r: not in result → add → result: "pr"
- o: not in result → add → result: "pro"
- g: not in result → add → result: "prog"
- r: already in result → skip
- a: not in result → add → result: "proga"
- m: not in result → add → result: "progam"
- m: already in result → skip
- i: not in result → add → result: "progami"
- n: not in result → add → result: "progamin"
- g: already in result → skip
FINAL: "progamin"
*/
// ARRAY-BASED APPROACH
// ----------------------------------------------------------------------------
const source = "javascript";
const uniqueArray = []; // Store unique characters in array
for (let i = 0; i < source.length; i++) {
let character = source[i];
let isDuplicate = false;
// Check if character already in array
for (let j = 0; j < uniqueArray.length; j++) {
if (uniqueArray[j] === character) {
isDuplicate = true;
break;
}
}
// Add if not duplicate
if (isDuplicate === false) {
uniqueArray[uniqueArray.length] = character; // Manual push
}
}
// Convert array back to string
let arrayResult = "";
for (let i = 0; i < uniqueArray.length; i++) {
arrayResult = arrayResult + uniqueArray[i];
}
console.log(arrayResult);
/*
ARRAY-BASED EXPLANATION:
"Alternative approach using array to collect unique characters"
WHY USE ARRAY APPROACH:
- Sometimes easier to work with arrays
- Can access individual unique characters later
- Shows different problem-solving approaches
- Good for follow-up questions about character positions
*/