-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
Signed-off-by: yourarj <[email protected]> Date: Fri Dec 29 08:17:02 2023 +0530 On branch main Changes to be committed: new file: .idea/misc.xml new file: .idea/modules.xml new file: .idea/rust-ds-algo.iml new file: .idea/vcs.xml new file: .idea/workspace.xml modified: Cargo.lock modified: Cargo.toml modified: leetcode-two-sum/src/main.rs new file: oracle-linked-list-unique-elements/Cargo.toml new file: oracle-linked-list-unique-elements/LinkedListDeDuplication.md new file: oracle-linked-list-unique-elements/src/lib.rs
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[package] | ||
name = "oracle-linked-list-unique-elements" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Deduplication in Singly-Linked-List | ||
## Asked by: Oracle | ||
|
||
Remove all instances of element from singly linked list so the resulting linked list will only contain elements whose | ||
occurrence was exactly once. | ||
|
||
e.g. | ||
Example 01 | ||
``` | ||
Input: 1 -> 1 -> 1 -> 2 -> 3 -> 3 -> 4 | ||
Output: 2 -> 4 | ||
``` | ||
|
||
Example 02 | ||
``` | ||
Input: 1 -> 1 -> 1 | ||
Output: null | ||
``` | ||
|
||
Example 03 | ||
``` | ||
Input: 1 -> 1 -> 1 -> 2 -> 3 -> 3 | ||
Output: 2 | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
pub fn deduplicate_linked_list(list: Vec<i32>) -> Vec<i32> { | ||
let mut output = vec![]; | ||
for index in 0..list.len() { | ||
let add_current = match ( | ||
if index == 0 { | ||
None | ||
} else { | ||
list.get(index - 1) | ||
}, | ||
list[index], | ||
list.get(index + 1), | ||
) { | ||
(None, _, None) => true, | ||
(None, current, Some(next)) => current != *next, | ||
(Some(last), current, Some(next)) => *last != current && current != *next, | ||
(Some(last), current, None) => *last != current, | ||
}; | ||
|
||
if add_current { | ||
output.push(list[index]) | ||
} | ||
} | ||
output | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::deduplicate_linked_list; | ||
|
||
#[test] | ||
fn test_deduplicate_linked_list() { | ||
assert_eq!( | ||
vec![2, 4], | ||
deduplicate_linked_list(vec![1, 1, 1, 2, 3, 3, 4]) | ||
); | ||
} | ||
|
||
// test 1 -> 1 -> 1 -> 2 -> 3 -> 3 -> 4 -> 4 | ||
#[test] | ||
fn test_deduplicate_linked_list2() { | ||
assert_eq!( | ||
vec![2], | ||
deduplicate_linked_list(vec![1, 1, 1, 2, 3, 3, 4, 4]) | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_remove_no_elements_from_all_unique_linked_list() { | ||
assert_eq!(vec![1, 2, 3, 4], deduplicate_linked_list(vec![1, 2, 3, 4])); | ||
} | ||
|
||
#[test] | ||
fn test_do_nothing_on_empty_linked_list() { | ||
assert!(deduplicate_linked_list(vec![]).is_empty()); | ||
} | ||
|
||
#[test] | ||
fn test_remove_everything_from_all_duplicated_linked_list() { | ||
assert!(deduplicate_linked_list(vec![100, 100]).is_empty()); | ||
} | ||
} |