diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..639900d --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..07f7e63 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/rust-ds-algo.iml b/.idea/rust-ds-algo.iml new file mode 100644 index 0000000..d6ebd48 --- /dev/null +++ b/.idea/rust-ds-algo.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..a11ace4 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,51 @@ + + + + + + + + + + + + + + + + + + + + + + 1694821809366 + + + + + + \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index d54ee77..2a7f473 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -130,6 +130,10 @@ version = "0.1.0" name = "merge-sort" version = "0.1.0" +[[package]] +name = "oracle-linked-list-unique-elements" +version = "0.1.0" + [[package]] name = "university-of-princeton-3sum" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index a60228c..1b877bd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -43,4 +43,5 @@ members = [ "leetcode-remove-duplicates-from-sorted-array", "leetcode-sqrtx", "leetcode-find-the-index-of-the-first-occurrence-in-a-string", + "oracle-linked-list-unique-elements", ] diff --git a/leetcode-two-sum/src/main.rs b/leetcode-two-sum/src/main.rs index 99dac9b..bb772d8 100644 --- a/leetcode-two-sum/src/main.rs +++ b/leetcode-two-sum/src/main.rs @@ -10,7 +10,7 @@ pub fn two_sum(nums: Vec, target: i32) -> Vec { let mut answer = Vec::new(); for (index, element) in nums.into_iter().enumerate() { if complement_map_with_index.contains_key(&element) { - answer.push(*complement_map_with_index.get(&element).unwrap().deref()); + answer.push(*complement_map_with_index.get(&element).unwrap()); answer.push(index as i32); break; } diff --git a/oracle-linked-list-unique-elements/Cargo.toml b/oracle-linked-list-unique-elements/Cargo.toml new file mode 100644 index 0000000..e8350f9 --- /dev/null +++ b/oracle-linked-list-unique-elements/Cargo.toml @@ -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] diff --git a/oracle-linked-list-unique-elements/LinkedListDeDuplication.md b/oracle-linked-list-unique-elements/LinkedListDeDuplication.md new file mode 100644 index 0000000..4613ee4 --- /dev/null +++ b/oracle-linked-list-unique-elements/LinkedListDeDuplication.md @@ -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 +``` \ No newline at end of file diff --git a/oracle-linked-list-unique-elements/src/lib.rs b/oracle-linked-list-unique-elements/src/lib.rs new file mode 100644 index 0000000..986ce0f --- /dev/null +++ b/oracle-linked-list-unique-elements/src/lib.rs @@ -0,0 +1,61 @@ +pub fn deduplicate_linked_list(list: Vec) -> Vec { + 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()); + } +}